Notification texts go here Contact Us Buy Now!

SQL Query to get last 4 Quarters with last 2 digit of Year

SQL Query to get last 4 Quarters with last 2 digit of Year

1. Using CROSS APPLY and VALUES():


SELECT CONCAT(DATEPART(QUARTER,Q.D),'Q',RIGHT(DATEPART(YEAR,Q.D),2))
FROM (VALUES(0),(-1),(-2),(-3)) V(i)
     CROSS APPLY (VALUES(DATEADD(QUARTER,V.i,GETDATE()))) Q(D);

2. Using CROSS JOIN and DATEPART():


SELECT CAST( DATEPART(q, DATEADD(q, -v.num, d.CurrentDate) ) AS VARCHAR(1)) 
+ 'Q'
+ RIGHT( CAST( DATEPART(yyyy, DATEADD(q, -v.num, d.CurrentDate)) AS VARCHAR(4) ), 2 )
FROM ( VALUES ( GETDATE() ) ) d(CurrentDate)
    CROSS JOIN ( VALUES (0), (1), (2), (3) ) v(num)
ORDER BY DATEADD(q, -v.num, d.CurrentDate) DESC

3. Using DATEADD() and DATEDIFF():


--QCurrent
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0) as QCurrentStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) as QLast

--LastQuarter
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) as LastQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0))) as nvarchar(6)) as QCurrent

--SecondQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) as SecondCurrent

--ThreeQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) as SecondCurrent

--FourQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) -4, 0))) as nvarchar(6)) as SecondCurrent

select datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) -4, 0)))

select format(getdate(), 'yy')

4. Using a Temporary Table:


--LAST FIVE QUARTERS TEMP TABLE
IF OBJECT_ID('tempdb..#lastfivequarters') IS NOT NULL DROP TABLE #lastfivequarters

CREATE TABLE #lastfivequarters (QOrdinal int, QName nvarchar(10)
    , QStartDate date, QEndDate date, QLabel nvarchar(10))

INSERT INTO #lastfivequarters
SELECT * FROM (
--Current Quarter
select
1 as QOrdinal
, 'Current' as QName
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0) as QStartDate
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0) as QEndDate
, 'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) as QLabel
UNION
--Last Quarter
select 
2 as QOrdinal
, 'Last' as QName
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) as QStartDate
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0) as QEndDate
, 'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0))) as nvarchar(6)) as QLabel
UNION
--Two Quarters Back
select 
3 as QOrdinal
, 'Second' as QName
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0) as QStartDate
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) as QEndDate 
, 'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) as QLabel
UNION
--Three Quarters Back
select 
4 as QOrdinal
, 'Third' as QName
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0) as QStartDate
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0) as QEndDate
, 'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) as QLabel
UNION
--Four Quarters Back
select 
5 as QOrdinal
, 'Fourth' as QName
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0) as QStartDate 
, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0) as QEndDate
, 'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) -4, 0))) as nvarchar(6)) as QLabel
) q

SELECT * FROM #lastfivequarters
DROP TABLE #lastfivequarters

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.