Return "1st Quarter" or "2nd Quarter"...
I was asked to write a single SQL SELECT statement that would return "1st Quarter" or "2nd Quarter" or "3rd Quarter" or "4th Quarter" based on a date column.
I know that I can call some SQL functions to cast the date into month and from there, I can see if it falls within a specific quarter of the year. However, I don't know how to return the value being asked for. This value ("1st Quarter" or "2nd Quarter" or "3rd Quarter" or "4th Quarter") is not contained in a column.
Anyone has an idea?
Re: Return "1st Quarter" or "2nd Quarter"...
would this be the answer?
Code:
SELECT case when (MONTH(date) >=1 AND MONTH(date) <=3) then "1st Quarter"
when (MONTH(date) >=4 AND MONTH(date) <=6) then "2nd Quarter"
when (MONTH(date) >=7 AND MONTH(date) <=9) then "3rd Quarter"
when (MONTH(date) >=10 AND MONTH(date) <=12) then "4th Quarter"
end as quarter
FROM table