peter
peter

Reputation: 2516

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?

Upvotes: 4

Views: 9423

Answers (3)

Deepak Revanaki
Deepak Revanaki

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

Jamie F
Jamie F

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

Sparky
Sparky

Reputation: 15075

You are almost there.

SELECT DateDiff(dd,CONVERT(DATE, 
       dateadd(qq, datediff(qq,0, GETDATE()),0)),getDate())+1

Upvotes: 2

Related Questions