I want to calculate the week number of the month,I want to calculate the week number whether its odd or even week how can I get this in TSQL ? Thanks all!
The WEEK() function returns the week number for a given date (a number from 0 to 53).
How Do You Group Data by Week in SQL Server? SQL Server provides a function called DATEPART() , which returns a specified part (year, quarter, month, week, hour, minute, etc.) of a specified date. ORDER BY DATEPART(week, RegistrationDate);
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.
This gives you the week of the date @dt within its month. There is a 2nd column that uses a CASE statement over the expression, to show either "Odd" or "Even"
declare @dt datetime
set @dt = GETDATE()
select
WhichWeekOfMonth = datepart(wk, @dt)
- datepart(wk,dateadd(m, DATEDIFF(M, 0, @dt), 0)) + 1,
case when (datepart(wk, @dt)
- datepart(wk,dateadd(m, DATEDIFF(M, 0, @dt), 0)) + 1) % 2 = 1
then 'Odd' else 'Even' end
It will give you week Number for Every month
declare @dates datetime
select @dates='2011-03-22'
SELECT datepart(dd,@dates), ceiling (cast(datepart(dd,@dates)as numeric(38,8))/7)
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