peter peter - 9 months ago 44
SQL Question

Getting Number of Day in Quarter

I am trying to get Which day of the quarter when you give a current date.

For Example if i give 01/25/2012 then the output should be 25. Since it is the 25th day of the quarter.

Similarly if i give 02/01/2012 it should give 32 as output.

I am able to get the first day of the quarter but not able to get which day it is in the quarter.

I did something like this:

SELECT FirstDayOfQuarter = CONVERT(DATE, dateadd(qq, datediff(qq,0, GETDATE()),0))

Can anyone help me?


Find the first day of the quarter: DATEADD(q, DATEDIFF(q, 0, @TestDate),0)

Then find the difference in days with your given date.

DECLARE @TestDate DateTime

SET @TestDate = 'January 25, 2012'

SELECT DATEDIFF(dd, DATEADD(q, DATEDIFF(q, 0, @TestDate),0), @TestDate) +1