In PostgreSQL you can use the EXTRACT()
function to get the month from a date.
You can also use the DATE_PART()
function to do the same thing.
Example 1: The EXTRACT() Function
Here’s an example of using the EXTRACT()
function to extract the month from a date.
SELECT EXTRACT( MONTH FROM TIMESTAMP '2020-12-16 10:41:35' ) AS "Month";
Result:
Month ------- 12
Here’s another example using the current timestamp.
SELECT current_timestamp, EXTRACT( MONTH FROM current_timestamp ) AS "Month";
Result:
current_timestamp | Month ------------------------------+------- 2020-03-05 09:15:19.89834+10 | 3
Example 2: The DATE_PART() Function
Here’s an example of using the DATE_PART()
function instead.
SELECT current_timestamp, DATE_PART( 'month', current_timestamp ) AS "Month";
Result:
current_timestamp | Month -------------------------------+------- 2020-03-05 09:16:53.587544+10 | 3