从日期中提取月份为字符串mm

时间:2018-02-16 16:46:15

标签: sql firebird

我想从日期栏中获取月份

EXTRACT(MONTH FROM date) Month

它给了我一个数字,但我需要一个像JAN,FEB等字符串。

要完成的SQL是什么?

1 个答案:

答案 0 :(得分:1)

Firebird中没有内置函数会立即生成月份名称。如果您需要,您需要自己编写,例如使用extractdecode的组合:

decode(
    extract(month from datevalue),
    1, 'JAN',
    2, 'FEB',
    3, 'MAR',
    4, 'APR',
    5, 'MAY',
    6, 'JUN',
    7, 'JUL',
    8, 'AUG',
    9, 'SEP',
    10, 'OCT',
    11, 'NOV',
    12, 'DEC')

如果您使用Firebird 3,则可以为此创建PSQL function

create function monthname(datevalue date) returns char(3)
as
begin
    return decode(
            extract(month from datevalue),
            1, 'JAN',
            2, 'FEB',
            3, 'MAR',
            4, 'APR',
            5, 'MAY',
            6, 'JUN',
            7, 'JUL',
            8, 'AUG',
            9, 'SEP',
            10, 'OCT',
            11, 'NOV',
            12, 'DEC');
end

或者,您可以尝试:

case extract(month from datevalue)
   when 1 then 'JAN'
   when 2 then 'FEB'
   -- etc
   when 12 then 'DEC'
end
相关问题