chaitanya chaitanya - 3 months ago 16
SQL Question

check month in quarter sql

How to check where particular month is in particular quarter of an year in sql db2.
like months 1-4 in quarter 1 like than i want to check whether month-1 is in which quarter of the year

Answer

try this one

SELECT
    MyDate,
    CASE
        WHEN MONTH(MyDate) BETWEEN 1  AND 3  THEN convert(char(4), YEAR(MyDate) - 1) + 'Q3'
        WHEN MONTH(MyDate) BETWEEN 4  AND 6  THEN convert(char(4), YEAR(MyDate) - 1) + 'Q4'
        WHEN MONTH(MyDate) BETWEEN 7  AND 9  THEN convert(char(4), YEAR(MyDate) - 0) + 'Q1'
        WHEN MONTH(MyDate) BETWEEN 10 AND 12 THEN convert(char(4), YEAR(MyDate) - 0) + 'Q2'
    END AS Quarter
FROM
    MyTable
order by Quarter

OR

SELECT
   date
   , CASE
         WHEN MONTH(date) BETWEEN 7 AND 9 THEN 'First Quarter'
         WHEN MONTH(date) BETWEEN 10 AND 12 THEN 'Second Quarter'
         WHEN MONTH(date) BETWEEN 1 AND 3 THEN 'Third Quarter'
         WHEN MONTH(date) BETWEEN 4 AND 6 THEN 'Fourth Quarter'
     END AS quarter
FROM Stuff
Comments