I have a table with a column month
(integer
). In this column I store values like 1, 2, .. 12
.
But I have to show the month name
There might be a quicker answer, but it seems to be possible by:
For example:
select to_char(to_timestamp(to_char(4, '999'), 'MM'), 'Mon')
returns 'Apr'.
You can turn it into a function:
create function to_month(integer) returns varchar as
$$
select to_char(to_timestamp(to_char($1, '999'), 'MM'), 'Mon');
$$ language sql
and use it throughout your code.
select to_month(month_column) from mytable
Basically what @small_duck already posted, but a couple of improvements:
SELECT to_char(to_timestamp (4::text, 'MM'), 'TMmon')
A plain cast to text 4::text
is enough, no need for to_char(..)
.
Question asks for lower case "jan", there is a template pattern for that: mon
.
If you want to localize the output, prefix the template with the modifier TM
.