Wednesday, June 6, 2012

Get Week Numbers for Quarter and Month in SQL...

I needed to get the Week Number for the Month and the Quarter for a BI Application.
I was able to retrieve it using the below query.

SELECT DATEDIFF(WEEK, DATEADD(MONTH, DATEDIFF(MONTH, 0, '2012-03-17'), 0), '2012-03-17') +1 AS WeekNoMonth,DATEDIFF(WEEK, DATEADD(QUARTER, DATEDIFF(QUARTER, 0, '2012-03-17'), 0), '2012-03-17') +1 AS WeekNoQuarter

The results are WeekNoMonth=3 and WeekNoQuarter=11

No comments:

Post a Comment