Extracting the Month from a Date in PostgreSQL

Avatar

By squashlabs, Last Updated: October 30, 2023

Extracting the Month from a Date in PostgreSQL

Understanding Date and Time in PostgreSQL

PostgreSQL is a useful open-source relational database management system that provides robust support for handling date and time data. In PostgreSQL, the date and time data types are represented by the date, time, timestamp, and interval types.

The date type represents a specific day, while the time type represents a specific time of day. The timestamp type combines both the date and time components, and the interval type represents a duration or time span.

When working with dates and times in PostgreSQL, it’s important to understand how they are stored and how to manipulate them using built-in functions and operators.

Related Article: How to Compare & Manipulate Dates in PostgreSQL

The EXTRACT Function in PostgreSQL

The EXTRACT function in PostgreSQL allows you to extract specific parts of a date or time value. It takes two arguments: the field to extract and the value from which to extract it. The field can be any valid date or time field, such as year, month, day, hour, minute, second, and so on.

Here’s an example that demonstrates how to use the EXTRACT function to extract the year from a date:

SELECT EXTRACT(year FROM '2022-01-01'::date);

This will return the year 2022.

Extracting the Month from a Date in PostgreSQL

To extract the month from a date in PostgreSQL, you can use the EXTRACT function with the month field. Here’s an example:

SELECT EXTRACT(month FROM '2022-01-01'::date);

This will return the month 1.

Retrieving the Month from a Date in PostgreSQL

In addition to using the EXTRACT function, PostgreSQL provides a more convenient way to retrieve the month from a date using the MONTH function. The MONTH function takes a date as an argument and returns the month as an integer.

Here’s an example that demonstrates how to use the MONTH function to retrieve the month from a date:

SELECT MONTH('2022-01-01'::date);

This will return the month 1.

Related Article: Working With PostgreSQL: Extracting Day of Week

The Month Function in PostgreSQL

The MONTH function in PostgreSQL is a shortcut for the EXTRACT function with the month field. It allows you to retrieve the month from a date without explicitly specifying the field.

Here’s an example that demonstrates how to use the MONTH function to retrieve the month from a date:

SELECT MONTH('2022-01-01'::date);

This will return the month 1.

Examples of Extracting the Month from a Date in PostgreSQL

Let’s look at some examples that demonstrate how to extract the month from a date in PostgreSQL using both the EXTRACT function and the MONTH function.

Example 1: Using the EXTRACT function

SELECT EXTRACT(month FROM '2022-01-01'::date) AS month;

Output:

 month
-------
     1

Example 2: Using the MONTH function

SELECT MONTH('2022-01-01'::date) AS month;

Output:

 month
-------
     1

Extracting the Month from a Timestamp in PostgreSQL

In addition to extracting the month from a date, you can also extract the month from a timestamp in PostgreSQL. The process is the same, using either the EXTRACT function or the MONTH function.

Here’s an example that demonstrates how to extract the month from a timestamp using the EXTRACT function:

SELECT EXTRACT(month FROM '2022-01-01 12:34:56'::timestamp) AS month;

This will return the month 1.

Related Article: Methods to Add Dates in PostgreSQL Databases

The Syntax for Extracting the Month from a Datetime in PostgreSQL

To extract the month from a datetime value in PostgreSQL, you can use the same syntax as extracting the month from a date or a timestamp. Here’s the syntax:

SELECT EXTRACT(month FROM datetime_expression) AS month;

Or, you can use the MONTH function with the datetime expression as the argument:

SELECT MONTH(datetime_expression) AS month;

Replace datetime_expression with the actual datetime value or column name you want to extract the month from.

Built-in Functions in PostgreSQL to Extract the Month from a Date

In addition to the EXTRACT function and the MONTH function, PostgreSQL provides several other built-in functions that can be used to extract the month from a date. These functions include DATE_PART, TO_CHAR, and DATE_TRUNC.

Here’s an example that demonstrates how to use the DATE_PART function to extract the month from a date:

SELECT DATE_PART('month', '2022-01-01'::date) AS month;

This will return the month 1.

SQL Query to Get the Month from a Date in PostgreSQL

To get the month from a date in PostgreSQL, you can use a simple SQL query with the SELECT statement and the EXTRACT function.

Here’s an example:

SELECT EXTRACT(month FROM date_column) AS month FROM table_name;

Replace date_column with the actual column name that contains the date values, and table_name with the name of the table you want to retrieve the month from.

Related Article: How to Set Timestamps With & Without Time Zone in PostgreSQL

Converting a Date to its Corresponding Month in PostgreSQL

To convert a date to its corresponding month in PostgreSQL, you can use the TO_CHAR function with the appropriate format specifier.

Here’s an example that demonstrates how to convert a date to its corresponding month:

SELECT TO_CHAR('2022-01-01'::date, 'Month') AS month;

This will return the month “January”.