Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 372
peter Avatar asked Dec 09 '22 03:12

peter


2 Answers

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;
like image 173
Jamie F Avatar answered Dec 29 '22 20:12

Jamie F


You are almost there.

SELECT DateDiff(dd,CONVERT(DATE, 
       dateadd(qq, datediff(qq,0, GETDATE()),0)),getDate())+1
like image 24
Sparky Avatar answered Dec 29 '22 20:12

Sparky