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.
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
DECLARE @date date = '2021-9-23'; SELECT DATEPART(week, @date) as 'Week Number';
Example 2: Alternatively, you can use
ww as the first argument to do the same thing and the output will be the same.
DECLARE @date date = '2022-09-23'; SELECT DATEPART(week, @date) AS week, DATEPART(wk, @date) AS wk, DATEPART(ww, @date) AS ww;
week wk ww
----------- ----------- -----------
39 39 39
As you can see in the above example, the output using the alternative Interval of
ww is the same as
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!