59

I have different dates in a column. For example:

20080102
20070821

I want to convert these dates in Year and calendar quarter. E.g.,

Year      Quarter
2008      2008-Q1
2007      2007-Q3

I can get the first column with:

select left(date,4) as year from table 

How can I produce the second column?

4
  • What is the datatype of your column? Jun 21, 2012 at 15:14
  • datatype for the date column is int
    – David
    Jun 21, 2012 at 15:30
  • 7
    Since you are storing the data incorrectly, the first thing you need to do is to check for invalid dates and fix them. It is a very poor idea to store dates as integers.
    – HLGEM
    Jun 21, 2012 at 17:35
  • 2
    @HLGEM blimey - our data warehouse is based on DateKeys as integers. Never really questioned having to covert them to char(8) all the time! SQLFiddle
    – whytheq
    Jun 22, 2012 at 21:11

19 Answers 19

136
SELECT DATEPART(QUARTER, @date)

This returns the quarter of the @date, assuming @date is a DATETIME.

4
  • The OP states that the column is of type INT, not DATETIME. This will not work for them. Aug 15, 2013 at 18:38
  • 6
    I had not seen that. I was looking for a solution to this for a DATETIME type, and did not see this anywhere on here. I put it here for anyone else looking for Quarter on a date who does have a datetime object.
    – DLeh
    Sep 17, 2013 at 17:07
  • This also returns the 1, 2, 3, or 4 only, not 2008-Q1 requested in the OP. There doesn't appear to be a succinct way to achieve what the OP asked.
    – Nickolay
    Mar 22, 2023 at 20:50
  • Clearly OP had figured out the year part already. So beyond that you just need to know (or be able to google) how to concat strings in sql. SELECT CONCAT(DATEPART(year, @date), '-', DATEPART(QUARTER, @date))
    – DLeh
    Mar 23, 2023 at 17:40
13
SELECT DATENAME(Quarter, CAST(CONVERT(VARCHAR(8), datecolumn) AS DATETIME))
1
  • SELECT DATENAME(Quarter, CONVERT(datetime, mycolumn )) from Mytable This throws an error --Arithmetic overflow error converting expression to data type datetime.
    – David
    Jun 21, 2012 at 16:08
8

Here's how I do it. Pretty brief and doesn't rely on temp tables.

CAST(year(TheDate) AS char(4)) + '-Q' + 
CAST(CEILING(CAST(month(TheDate) AS decimal(9,2)) / 3) AS char(1))

As an example:

SELECT convert(char(10), getdate(), 101) AS TheDate, 
CAST(year(getdate()) AS char(4)) + '-Q' + 
    CAST(CEILING(CAST(month(getdate()) AS decimal(4,2)) / 3) AS char(1)) AS SelectQuarter 

This will return:

TheDate    SelectQuarter
---------- -------------
07/10/2013 2013-Q3

Obviously the string itself can be changed to suit your own format. Hope this is helpful.

3

I did it like this (I am using SQL Server):

SELECT 'Q' + cast(DATEPART(QUARTER, GETDATE()) as varchar(1)) + ' - ' + cast(DATEPART(YEAR, GETDATE()) as varchar(4)) AS 'Date Quarter'
2

Since your date field data is in int you will need to convert it to a datetime:

declare @date int
set @date = 20080102

SELECT Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + substring(cast(@date as char(8)), 7, 2) as datetime)) as Quarter

or

SELECT Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + right(@date, 2) as datetime)) as quarter

Then if you want the Q1 added:

SELECT left(@date, 4) + '-Q' + Convert(varchar(1), Datename(quarter, Cast(left(@date, 4) + '-' 
    + substring(cast(@date as char(8)), 5, 2) + '-' 
    + right(@date, 2) as datetime))) as quarter

My advice would be to store your date data as a datetime so then you do not need to perform these conversions.

2

Assuming field data type INT and field name "mydate". In the OP question, the INT date values when converted to string are ISO date literals.

select DatePart(QUARTER, cast(cast(mydate as char(8)) as date))

You can use datetime if using older server version.

2

Here is another option. Use a CTE to define the months of the quarter and then join to it to determine the quarter:

WITH Quarters AS (
   SELECT Q = 'Q1', MonthBegin = 1, MonthEnd = 3 UNION
   SELECT Q = 'Q2', MonthBegin = 4, MonthEnd = 6 UNION
   SELECT Q = 'Q3', MonthBegin = 7, MonthEnd = 9 UNION
   SELECT Q = 'Q4', MonthBegin = 10, MonthEnd = 12
)
SELECT
   [Year] = DATEPART(yyyy, CONVERT(DATETIME, Dates.[date])),
   [Quarter] = CONVERT(VARCHAR(4), DATEPART(yyyy, CONVERT(DATETIME, Dates.[date]))) + '-' + q.Q
FROM
   (VALUES
       ('20080102'),
       ('20070821')
   ) AS Dates ([date])
   INNER JOIN Quarters q ON
      DATEPART(m, CONVERT(DATETIME, Dates.[date])) >= q.MonthBegin AND
      DATEPART(m, CONVERT(DATETIME, Dates.[date])) <= q.MonthEnd;

Returns:

Year  Quarter
----- ----------
2008  2008-Q1
2007  2007-Q3

SQL Fiddle

Handle column type of int (04/23/2014):

WITH Quarters AS (
    SELECT Q = 'Q1', MonthBegin = 1, MonthEnd = 3 UNION
    SELECT Q = 'Q2', MonthBegin = 4, MonthEnd = 6 UNION
    SELECT Q = 'Q3', MonthBegin = 7, MonthEnd = 9 UNION
    SELECT Q = 'Q4', MonthBegin = 10, MonthEnd = 12
)
SELECT
    [Year] = DATEPART(yyyy, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))),
    [Quarter] = CONVERT(VARCHAR(4), DATEPART(yyyy, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date])))) + '-' + q.Q
FROM
    (VALUES
        (20080102),
        (20070821)
    ) AS Dates ([date])
    INNER JOIN Quarters q ON
        DATEPART(m, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))) >= q.MonthBegin AND
        DATEPART(m, CONVERT(DATETIME, CONVERT(VARCHAR(8), Dates.[date]))) <= q.MonthEnd;
2
  • i think when you do one more "denormalize" the quarter table and have a row for each month (1: Q1, 2: Q1, 3: Q1, 4: Q2, ... ) then you can direct join the month and not need a < and >
    – muescha
    Aug 29, 2022 at 12:14
  • you can also use the BETWEEN operator instead of <= and >=
    – muescha
    Aug 29, 2022 at 12:17
2

To get the exact output you requested, you can use the below:

CAST(DATEPART(YEAR, @Date) AS NVARCHAR(10)) + ' - Q' + CAST(DATEPART(QUARTER, @Date) AS NVARCHAR(10))

This will give you an outputs like: "2015 - Q1", "2013 - Q3", etc.

1

nice excuse to muck around with CONVERT. Probably prettier ways of doing it:

live test on SQLfiddle here

create table the_table 
(
  [DateKey] INT,
)

insert into the_table
values
(20120101),
(20120102),
(20120201),
(20130601)


WITH myDateCTE(DateKey, Date) as
  (
    SELECT 
      DateKey
      ,[Date] = CONVERT(DATETIME, CONVERT(CHAR(8),DateKey),112) 
    FROM the_table
   )

SELECT 
  t.[DateKey]
  , m.[Date]
  , [QuarterNumber] = CONVERT(VARCHAR(20),Datepart(qq,Date))
  , [QuarterString] = 'Q' + CONVERT(VARCHAR(20),Datepart(qq,Date))
  , [Year] = Datepart(yyyy,Date) 
  , [Q-Yr] = CONVERT(VARCHAR(2),'Q' + CONVERT(VARCHAR(20),Datepart(qq,Date))) + '-' + CONVERT(VARCHAR(4),Datepart(yyyy,Date))  
FROM 
  the_table t
  inner join myDateCTE m
    on 
    t.DateKey = m.DateKey
1
SELECT
   Q.DateInQuarter,
   D.[Year],
   Quarter = D.Year + '-Q'
      + Convert(varchar(1), ((Q.DateInQuarter % 10000 - 100) / 300 + 1))
FROM
   dbo.QuarterDates Q
   CROSS APPLY (
      VALUES (Convert(varchar(4), Q.DateInQuarter / 10000))
   ) D ([Year])
;

See a Live Demo at SQL Fiddle

2
  • can you tell me what will bee the calculation for 11_2015 this type of input?
    – SaNa3819
    Apr 10, 2016 at 11:44
  • @SaNa3819 Please ask a new question. This answer is in response to an int column. It can't accept string input. You're barking up the wrong tree.
    – ErikE
    Apr 10, 2016 at 15:19
0

Try this one

SELECT  CONCAT (TO_CHAR(sysdate,'YYYY-'),concat ('Q',TO_CHAR(sysdate,'Q') ))from dual

Replace sysdate with your own column name with date type format and dual with your table name

0
0

You have to convert the integer to a char(8) then a datetime. then wrap that in SELECT DATEPART(QUARTER, [date])

You will then have to convert the above to character and add on the '-' + year (also converted to char)

The arithmetic overflow above is caused by omitting the initial convert to a character type.

I would be inclined to abstract the conversion to date-time using views where possible and then use the quarter function and character conversion as and when required.

0

Here you see one of the more alternatives :

SELECT CASE
         WHEN @TODAY BETWEEN @FY_START AND DATEADD(M, 3, @FY_START) THEN 'Q1'
         WHEN @TODAY BETWEEN DATEADD(M, 3, @FY_START) AND DATEADD(M, 6, @FY_START) THEN 'Q2'
         WHEN @TODAY BETWEEN DATEADD(M, 6, @FY_START) AND DATEADD(M, 9, @FY_START) THEN 'Q3'
         WHEN @TODAY BETWEEN DATEADD(M, 9, @FY_START) AND DATEADD(M, 12, @FY_START) THEN 'Q4'
       END
0

Using the function MONTH which returns the month as a number, we can easily calculate the quarter.

select date, CEILING((MONTH(date) * 4) / 12) quarter from dual
0
declare @TempTable table([Date] datetime)

insert into @TempTable([Date])
values('2008-01-02'),('2007-08-21')
                      
select datepart(year, [Date]) as [year]
,convert(varchar(10),datepart(year, [Date])) + '-Q' + convert(varchar(3),datename(quarter, [Date])) as quarter_with_year
,datefromparts(datepart(year, [Date]),(convert(varchar(3),datename(quarter, [Date])) * 3)-2,1) as quarter_startdate
,eomonth(datefromparts(datepart(year, [Date]),convert(varchar(3),datename(quarter, [Date])) * 3,1)) as quarter_enddate
FROM @TempTable

This returns the year,quarter, and start end date of quarter.

0
select 
  your_date, 
  CONCAT (TO_CHAR(your_date, 'YYYY-'),concat ('Q', date_part(quarter, your_date) ))
from table;
0
DECLARE     @QuarterVar TINYINT
SELECT      @QuarterVar = ( ( ( DATEPART( month, GETDATE( ) ) - 1 ) / 3 ) + 1 )
0

This might help someone,looking for a way to get date from and date to for each quarter

declare @sdate date = '2022-02-01'
    , @edate date = '2023-12-31';

with dates_CTE (cDate) as (
    select @sdate
    Union ALL
    select DATEADD(day, 1, cDate)
    from dates_CTE
    where cDate < @edate
)

select min( cDate) dateFrom,max(cDate) dateTo,  min(DATEPART(QUARTER, cDate))q, min(YEAR(cDate)) qYear, CONCAT(YEAR(cDate),'-Q', DATEPART(QUARTER, cDate)) qName
from dates_CTE
group by DATEPART(QUARTER, cDate), YEAR(cDate)
option (maxrecursion 1000)

Sample out put

dateFrom dateTo q qYear qName
2022-02-01 2022-03-31 1 2022 2022-Q1
2022-04-01 2022-06-30 2 2022 2022-Q2
2022-07-01 2022-09-30 3 2022 2022-Q3
2022-10-01 2022-12-31 4 2022 2022-Q4
2023-01-01 2023-03-31 1 2023 2023-Q1
2023-04-01 2023-06-30 2 2023 2023-Q2
2023-07-01 2023-09-30 3 2023 2023-Q3
2023-10-01 2023-12-31 4 2023 2023-Q4
-3

Try the following:

CONCAT(datepart(yyyy,DATE),'-', DATEPART(qq,DATE))

It returns:

yyyy-q

Example: 2017-3 for 2017-07-11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.