How do I get the week number from dates in T-SQL?
MySQL WEEKOFYEAR() Function The WEEKOFYEAR() function returns the week number for a given date (a number from 1 to 53). Note: This function assumes that the first day of the week is Monday and the first week of the year has more than 3 days. Tip: Also look at the WEEK() function.
WEEK() function in MySQL is used to find week number for a given date. If the date is NULL, the WEEK() function will return NULL. Otherwise, it returns the value of week which ranges between 0 to 53. The date or datetime from which we want to extract the week.
SELECT DATEADD(week, DATEDIFF(week, 0, RegistrationDate - 1), 0) AS Monday; In the expression above, we add the specified number of weeks to the 0 date. As you remember, 0 represents midnight on Monday, 1 January 1900.
Have a look at DATEPART
SELECT DATEPART(wk, GETDATE())
It is best to use the following:
select DATEPART(ISO_WEEK, getDate())
As when you have a year with a week 53 as in the case of 2015 it give unreliable results. (Certainly on 2008 R2)
select DATEPART(WK, '01/03/2016')
Gives variable results around week 53. When run the week after 3rd Jan it produced the value 1. When run now for the same date it gives the value 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With