Quantcast
Channel: Scripts – SqlHints.com
Viewing all articles
Browse latest Browse all 28

How to get Quarter’s Start and End Date for a given date in Sql Server

$
0
0

We can use a query like below to get Quarter’s Start and End Date for a given date.

DECLARE @AnyDate DATETIME
SET @AnyDate = GETDATE()

SELECT @AnyDate AS 'Input Date',
  DATEADD(q, DATEDIFF(q, 0, @AnyDate), 0) 
                        AS 'Quarter Start Date',       
  DATEADD(d, -1, DATEADD(q, DATEDIFF(q, 0, @AnyDate) + 1, 0)) 
                        AS 'Quarter End Date'

Quarter_Start_End_Date_In_SqlServer

Note the Quarter End Date returned by the above query is without time part. If we need to generate a report for a quarter then we need to include all the transactions happening on the last date of the quarter till mid night. Then in the query instead of comparing with last date of the quarter we can check whether it is less than the next quarter start date.

Below query shows how to get Quarter’s Start Date and the Next Quarter Start Date for a given date.

DECLARE @AnyDate DATETIME
SET @AnyDate = GETDATE()

SELECT @AnyDate AS 'Input Date',
	DATEADD(q, DATEDIFF(q, 0, @AnyDate), 0) 
                               AS 'Quarter Start Date',       
	DATEADD(q, DATEDIFF(q, 0, @AnyDate) + 1, 0) 
                               AS 'Next Quarter Start Date'

Quarter_Start_And_Next_Quarter_Start_Date


Viewing all articles
Browse latest Browse all 28

Latest Images

Trending Articles



Latest Images