sgmunson (9/3/2015)
ramrajan (9/3/2015)
-- Find Quarter difference countdeclare @enddate datetime = '2015-09-03 00:39:53.973'
declare @startdate datetime = '2014-11-07 00:42:39.920'
WITH Quarters AS (
SELECT Q = '1', MonthBegin = 1, MonthEnd = 3 UNION
SELECT Q = '2', MonthBegin = 4, MonthEnd = 6 UNION
SELECT Q = '3', MonthBegin = 7, MonthEnd = 9 UNION
SELECT Q = '4', MonthBegin = 10, MonthEnd = 12
)
SELECT * FROM Quarters
-- I need to find total count difference between start date and enddate
@startdate - Fourth Quarter - 2014
@enddate - Three Quarter - 2015
Here in this case i should get quarter difference count as 4 . So how to get the difference count
Of far more importance than the "how", is the fact that you are expecting a result of 4. Run the following:
declare @startdate datetime = '2014-11-07 00:42:39.920'
declare @enddate datetime = '2015-09-03 00:39:53.973'
SELECT DATEDIFF(quarter, @startdate, @enddate) AS QUARTER_DIFFERENCE
The result that SQL Server thinks is the difference in quarters between those two dates is 3, and SQL Server is correct.
So my question is, why do you want 4 as the answer? If you are going to count the quarter that the @startdate is in,
then all you need to do is add 1 to the result, as follows:
declare @startdate datetime = '2014-11-07 00:42:39.920'
declare @enddate datetime = '2015-09-03 00:39:53.973'
SELECT DATEDIFF(quarter, @startdate, @enddate) + 1 AS QUARTER_DIFFERENCE
Let us know what works for you.
+1000.
I wonder how many quarters the OP would want for the following...
declare @startdate datetime = '2015-03-31 23:59:59.097';
declare @enddate datetime = '2015-04-01 00:00:00.000';
--Jeff Moden
Change is inevitable... Change for the better is not.