Reputation: 2516
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?
Upvotes: 4
Views: 9423
Reputation: 11
Try This to get number of days in a CURRENT QUARTER;
Select datediff(dd,
dateadd(qq,
datediff(qq,0, getdate()),0),
dateadd(qq,
datediff(qq,0, getdate()) + 1,0))
Upvotes: 1
Reputation: 23789
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;
Upvotes: 11
Reputation: 15075
You are almost there.
SELECT DateDiff(dd,CONVERT(DATE,
dateadd(qq, datediff(qq,0, GETDATE()),0)),getDate())+1
Upvotes: 2