PostgreSQL has built-in methods for translating time strings, so it is unnecessary to create custom methods. You can use the to_char
function, passing the extracted date as the first parameter, as the second parameter, the string 'Month'
.
This will return the name of the month with the first letter capitalized (there is also 'MONTH' and 'month', if you prefer), according to the parameter lc_time
defined in its postgresql.conf
. For my case, where lc_time
is C
:
postgres=# select to_char(current_date, 'Month');
to_char
-----------
March
(1 row)
If you want the name of the month in another language without having to change the .conf, you can set lc_time
to its own session and use the string 'TMMonth'
. "TM" stands for "Translation Mode":
postgres=# set lc_time TO 'pt_BR.UTF-8';
SET
postgres=# select to_char(current_date, 'TMMonth');
to_char
---------
Março
(1 row)
postgres=# set lc_time TO 'fr_FR.UTF-8';
SET
postgres=# select to_char(current_date, 'TMMonth');
to_char
---------
Mars
(1 row)
postgres=# set lc_time TO 'ja_JP.UTF-8';
SET
postgres=# select to_char(current_date, 'TMMonth');
to_char
---------
3月
(1 row)
So, complementing the initial query of your question, try with:
select to_char(extract('Month',data), 'Month');
Or, if your PostgreSQL is not in Portuguese, use:
set lc_time to 'pt_BR.UTF-8';
select to_char(extract('Month',data), 'TMMonth');