Format the Month in Roman Numerals in PostgreSQL

In PostgreSQL, you can use the to_char() function to return dates in various formats.

One of the things you can do with this function is return the month portion of a date in roman numerals.

Example 1

Here’s an example to show you what I mean.

SELECT to_char(date '2020-07-15', 'RM');

Result:

VII

VII is the roman numeral equivalent to 7.

Example 2 – All Months

Here’s a list of all months converted to their roman numeral equivalent.

SELECT 
  to_char(date '2020-01-15', 'RM') AS "January",
  to_char(date '2020-02-15', 'RM') AS "February",
  to_char(date '2020-03-15', 'RM') AS "March",
  to_char(date '2020-04-15', 'RM') AS "April",
  to_char(date '2020-05-15', 'RM') AS "May",
  to_char(date '2020-06-15', 'RM') AS "June",
  to_char(date '2020-07-15', 'RM') AS "July",
  to_char(date '2020-08-15', 'RM') AS "August",
  to_char(date '2020-09-15', 'RM') AS "September",
  to_char(date '2020-10-15', 'RM') AS "October",
  to_char(date '2020-11-15', 'RM') AS "November",
  to_char(date '2020-12-15', 'RM') AS "December";

Result (using vertical output):

January   | I   
February  | II  
March     | III 
April     | IV  
May       | V   
June      | VI  
July      | VII 
August    | VIII
September | IX  
October   | X   
November  | XI  
December  | XII 

This example uses vertical output (also referred to as “expanded display”).

To set your output to expanded display in psql, use the following:

\x

Result:

Expanded display is on.

This code toggles it on and off. So to turn it off, simply enter it again.

\x

Result:

Expanded display is off.