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

I need to get the last 4 records from a table and it has records with column value like '19Q1','18Q4'.

using below query I can get last 4 quarters but is there any simplest form of this query.

SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,0,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,0,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-1,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-1,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-2,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-2,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-3,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-3,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period

O/P:

1Q19
4Q18
3Q18
2Q18

Assuming you're using 2012 (because you've tagged 3 different versions of SQL Server...) How about:

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);

SQL - DATE and TIME Functions, Assuming you're using 2012 (because you've tagged 3 different versions of SQL Server) How about: SELECT CONCAT(DATEPART(QUARTER,Q.D),'Q'� 2 = Monday; 3 = Tuesday; 4 = Wednesday; 5 = Thursday; 6 = Friday; 7 = Saturday; firstdayofyear: Optional. Specifies the first week of the year. Can be one of the following values: 0 = Use the NLS API setting; 1 = Use the first week that includes Jan 1st (default) 2 = Use the first week in the year that has at least 4 days; 3 = Use the first

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

SQL date format Overview; DateDiff SQL function, DateAdd SQL , There are 4 main ways to store date values in a PostgreSQL database: Dates in a database aren't stored as strings, but we input and fetch data from it as if it were a string with the following format for the SELECT TIMESTAMP '2020-07- 28 16:58:22'; YY, last 2 digits of the Year, TO_CHAR(DATE '2020-07-28', 'YY'), 20. Definition and Usage. The QUARTER() function returns the quarter of the year for a given date value (a number from 1 to 4). January-March returns 1

Gets Current Quarter start date going back 5 quarters plus Qx - format year

--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')

Simplify Date Period Calculations in SQL Server, For professionals just getting started with SQL Server, these Query the HireDate column to return only the year portion of the date 1. 2. 3. SELECT DATEPART(QUARTER, HireDate) AS [Quarter Part] The following example returns the number of years between the first hire date and the last hire date. i need syntax for extracting only last 4 digit, the problem is the string im working on are not uniformed in length here is the sample data below emp_ID-----12345678 1234567 123456 12345 needed result must be emp_ID-----5678 4567 3456: 2345

Solved: How to get the last two digits of the current year, In this tip we look at different SQL Server date functions to assist with needs a leading digit – just give me the first day for the year/month of the SELECT DATEFROMPARTS(YEAR(@today), 1, 1); Here is an example that queries the sys.databases table to find databases created in the last quarter. I need to get the last 4 records from a table and it has records with column value like '19Q1','18Q4'. using below query I can get last 4 quarters but is there any simplest form of this query.

Solved: getting last 2 digits of fiscal year, Monday) to get to the current week number, the value is 14 for today. But I want to get as 1914, is it possible? if no, I think I can get the last two digits of the year, such as 2019, Column in Power Query to get the last two digits of the current year or Message 2 of 3 Super Users of the Quarter - Q2 2020. Hello, I was wondering if there is a good way to find Last 4 quarters (excluding the current quarter). So If today is Oct 30th 2008, it should show 2007-Q4, 2008-Q1, 2008-Q2, 2008-Q3. I have a table whose column (Varchar2) in Oracle 8i has values in this format (YYYY-Q1) ie. 2008-Q1, 2008-Q2 and i want to get data where i find the match.

SQL Server YEAR() Function, Solved: hi all, maybe it a silly question but how i do get the last 2 digit of getting last 2 digits of fiscal year The Golden Rules for Power BI. Notice that we have the option of formatting the year portion as five digits. About the Number of Digits. When returning a datepart in digit form, the format specifier determines the minimum number of digits to return. For example, when using yyy, the year will be returned as three digits if the year is 0008 but as four digits if the year is 2008.