0

I am trying to get the month from a date column

EXTRACT(MONTH FROM date) Month

It gives me a number but I need a string like JAN, FEB, etc.

What is the SQL to accomplish that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

1

There is no built-in function in Firebird that will immediately yield a month name. If you want that, you'll need to write it yourself, for example using a combination of extract and decode:

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')

If you use Firebird 3, then you can create a PSQL function for this:

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

Alternatively, you could try:

case extract(month from datevalue)
   when 1 then 'JAN'
   when 2 then 'FEB'
   -- etc
   when 12 then 'DEC'
end
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Hi Mark, the code seems to be the solution but I have a problem, I get a message that it cannot convert the data type, I say 1 to 'JAN', If I convert 1 to 1 I get it right. – Jonathan Livingston Seagull Feb 16 '18 at 19:21
  • @JonathanLivingstonSeagull It works for me, what version of Firebird are you using? – Mark Rotteveel Feb 17 '18 at 09:37
  • @JonathanLivingstonSeagull you do not need to CONVERT anything in a straightforward query. It seems you try to integrate this solution into some larger framework of yours, maybe view or something. Show the exact query u execute and exact error message – Arioch 'The Feb 19 '18 at 13:48
  • Hello Mark, I am using version 2.5. The query is: decode(cast(extract(month from date) as int),'1','JAN','2', 'FEV') month, the erro " it is not possible to convert the type of data requested..." – Jonathan Livingston Seagull Feb 19 '18 at 15:58
  • @JonathanLivingstonSeagull 1) the cast to int is unnecessary, and 2) the numerical parameters should be a numerical literal (not a string literal), as shown in my answer. Point 2 shouldn't matter, it works for me if I use a string literal (Firebird 2.5.8, 3.0.3). However, the error message you quote is not a Firebird error message, which may mean your problem is elsewhere. – Mark Rotteveel Feb 19 '18 at 16:06
  • Hi Mark, decode(extract(month from date),2,2) month - it works. decode(extract(month from date),2,'feb') month - it does not work. I am using a connect via excel powerrpivot. – Jonathan Livingston Seagull Feb 19 '18 at 16:22
  • @JonathanLivingstonSeagull I don't know that, but maybe you're trying to request it as the wrong datatype in that tool? – Mark Rotteveel Feb 19 '18 at 16:32
  • It is likely that I am having a data type issue because when I use only numbers it works but when I change to a string it gives me an error. Do you think I can write a code to force the data types to be the same ? – Jonathan Livingston Seagull Feb 19 '18 at 17:34
  • @JonathanLivingstonSeagull Given I know nothing about Excel Powerpivot, I really can't answer that. – Mark Rotteveel Feb 19 '18 at 17:45