检查季度sql中的月份

时间:2016-08-30 07:31:50

标签: sql db2

如何在sql db2中检查特定月份的特定季度。 比如第1季的1-4个月比我想检查第1个月是否在一年中的哪个季度

2 个答案:

答案 0 :(得分:1)

DB2有一个内置的QUARTER function可以完成我想要的任务:

SELECT QUARTER(CURRENT TIMESTAMP) FROM SYSIBM.SYSDUMMY1

答案 1 :(得分:0)

尝试这个

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