In PostgreSQL, you can use the to_char()
function to get the short month name from a date.
By “short month name” I mean the abbreviated month name, for example Jan, Feb, Mar, etc.
The to_char()
function accepts two parameters; the date and the format string with which to format that date.
Example
Here’s an example to demonstrate.
SELECT to_char(current_timestamp, 'mon');
Result:
jun
This shows that it was June when I ran that query.
Specify the Case
You can specify whether or not the short month name should be in uppercase, lowercase, or title case.
To do this, simply use the desired case in the format string.
SELECT
to_char(current_timestamp, 'mon') AS "mon",
to_char(current_timestamp, 'Mon') AS "Mon",
to_char(current_timestamp, 'MON') AS "MON";
Result:
mon | Mon | MON -----+-----+----- jun | Jun | JUN
Longer Format String
You can include the short month name as part of a longer date output.
For example, the following.
SELECT to_char(current_timestamp, 'Dy, DD Mon YYYY');
Result:
Sun, 07 Jun 2020
Get the Long Month Name
You can use Month
, MONTH
, or month
to get the long month name.