CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2006
    Posts
    29

    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?

  2. #2
    Join Date
    May 2006
    Posts
    29

    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
  •  





Click Here to Expand Forum to Full Width

Featured