-
March 29th, 2012, 10:04 PM
#1
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?
-
March 29th, 2012, 10:15 PM
#2
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|