How to Convert a Month Number to Month Name in PostgreSQL

Introduction

PostgreSQL is a powerful and open source relational database management system that supports many advanced features. One of these features is the ability to manipulate dates and times using various functions and operators. In this article, we will learn how to convert a month number to a month name in PostgreSQL using the to_char function.

The to_char Function

The to_char function is a versatile function that can convert any value to a formatted text representation. It takes two arguments: the value to be converted and a format string that specifies how the value should be displayed. The format string can contain various placeholders that represent different parts of the value, such as year, month, day, hour, minute, second, etc. For example, the format string 'YYYY-MM-DD' will display a date value as 2023-11-27.

To convert a month number to a month name, we can use the to_char function with the format string 'Month'. This will display the full name of the month, such as November. If we want to display the abbreviated name of the month, we can use the format string 'Mon', which will display the first three letters of the month, such as Nov. Note that the format string is case-sensitive, so 'month' and 'mon' will display the month name in lowercase, such as november and nov.

Example of to_char Function

Here is an example of how to use the to_char function to convert a month number to a month name in PostgreSQL.

-- Create a table with some sample data
CREATE TABLE sales (
  id SERIAL PRIMARY KEY,
  product VARCHAR(50),
  quantity INT,
  price NUMERIC(10, 2),
  date DATE
);

-- Insert some sample data
INSERT INTO sales (product, quantity, price, date)
VALUES
('Laptop', 2, 50000, '2023-01-15'),
('Mobile', 3, 15000, '2023-02-20'),
('Tablet', 1, 20000, '2023-03-25'),
('Headphone', 4, 3000, '2023-04-30'),
('Speaker', 2, 5000, '2023-05-05'),
('Keyboard', 3, 1000, '2023-06-10'),
('Mouse', 5, 500, '2023-07-15'),
('Monitor', 1, 10000, '2023-08-20'),
('Printer', 2, 15000, '2023-09-25'),
('Scanner', 1, 10000, '2023-10-30'),
('Webcam', 3, 2000, '2023-11-05'),
('Microphone', 2, 3000, '2023-12-10');

-- Convert the month number to the full month name
SELECT id, product, quantity, price, date, to_char(date, 'Month') AS month_name
FROM sales;

-- Output
 id |  product  | quantity |  price  |    date    | month_name 
----+-----------+----------+---------+------------+------------
  1 | Laptop    |        2 | 50000.0 | 2023-01-15 | January   
  2 | Mobile    |        3 | 15000.0 | 2023-02-20 | February  
  3 | Tablet    |        1 | 20000.0 | 2023-03-25 | March     
  4 | Headphone |        4 |  3000.0 | 2023-04-30 | April     
  5 | Speaker   |        2 |  5000.0 | 2023-05-05 | May       
  6 | Keyboard  |        3 |  1000.0 | 2023-06-10 | June      
  7 | Mouse     |        5 |   500.0 | 2023-07-15 | July      
  8 | Monitor   |        1 | 10000.0 | 2023-08-20 | August    
  9 | Printer   |        2 | 15000.0 | 2023-09-25 | September 
 10 | Scanner   |        1 | 10000.0 | 2023-10-30 | October   
 11 | Webcam    |        3 |  2000.0 | 2023-11-05 | November  
 12 | Microphone|        2 |  3000.0 | 2023-12-10 | December  
(12 rows)

-- Convert the month number to the abbreviated month name
SELECT id, product, quantity, price, date, to_char(date, 'Mon') AS month_name
FROM sales;

-- Output
 id |  product  | quantity |  price  |    date    | month_name 
----+-----------+----------+---------+------------+------------
  1 | Laptop    |        2 | 50000.0 | 2023-01-15 | Jan       
  2 | Mobile    |        3 | 15000.0 | 2023-02-20 | Feb       
  3 | Tablet    |        1 | 20000.0 | 2023-03-25 | Mar       
  4 | Headphone |        4 |  3000.0 | 2023-04-30 | Apr       
  5 | Speaker   |        2 |  5000.0 | 2023-05-05 | May       
  6 | Keyboard  |        3 |  1000.0 | 2023-06-10 | Jun       
  7 | Mouse     |        5 |   500.0 | 2023-07-15 | Jul       
  8 | Monitor   |        1 | 10000.0 | 2023-08-20 | Aug       
  9 | Printer   |        2 | 15000.0 | 2023-09-25 | Sep       
 10 | Scanner   |        1 | 10000.0 | 2023-10-30 | Oct       
 11 | Webcam    |        3 |  2000.0 | 2023-11-05 | Nov       
 12 | Microphone|        2 |  3000.0 | 2023-12-10 | Dec       
(12 rows)

Conclusion

As you can see, the to_char function is very useful for converting a month number to a month name in PostgreSQL. You can also use it to format other parts of the date and time value, such as the day of the week, the hour, the minute, the second, etc. This is one of the many ways to get the month name from a date in PostgreSQL. You can also try other ways to get the same result.

I hope you enjoyed this article and learned something new. Thank you for reading.


Similar Articles