;

How to Get or Extract the Week Number from a Date in SQL Server


Tutorialsrack 23/02/2021 SQL SQL Server

In this article, you will learn how to get or extract the week number of a year from a date in SQL Server. You can use the DATEPART() function to get the week number of a year from a date.

Here is the example to extract the week number from a date in SQL Server.

DATEPART() Function

This function returns a specified part of a date. And this function returns an output as an integer value. This function takes the two parameters: the first parameter is interval and the second parameter is the date. Both parameters are required.

Example 1: To return the week number of the year from a date, use the week as the first argument of the DATEPART() function. 

Example 1
DECLARE @date date = '2021-9-23';
SELECT DATEPART(week, @date) as 'Week Number';
Output

Week Number

-----------

39

Example 2: Alternatively, you can use wk or ww as the first argument to do the same thing and the output will be the same.

Example 2
DECLARE @date date = '2022-09-23';
SELECT 
    DATEPART(week, @date) AS week,
    DATEPART(wk, @date) AS wk,
    DATEPART(ww, @date) AS ww;
Output

week        wk          ww

----------- ----------- -----------

39          39          39

As you can see in the above example, the output using the alternative Interval of WEEK, wk and ww is the same as WEEK interval.

I hope this article will help you to understand how to get or extract the week number of a year from a date in SQL Server(T-SQL).

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags