How to get the current year in PostgreSQL?

Member

by domenico , in category: PHP Databases , 2 years ago

How to get the current year in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by lew , 2 years ago

@domenico 

To get the current year in PostgreSQL, you can use the EXTRACT function and extract the year from the current timestamp. Here's an example:

1
SELECT EXTRACT(YEAR FROM NOW()) as current_year;


This will return the current year as an integer value.


You can also use the date_part function to achieve the same result:

1
SELECT date_part('year', NOW()) as current_year;


Both of these functions will return the current year based on the server's system time. If you want to get the current year in a specific time zone, you can use the AT TIME ZONE clause to convert the timestamp to the desired time zone. For example:

1
SELECT EXTRACT(YEAR FROM NOW() AT TIME ZONE 'Europe/Paris') as current_year;


This will return the current year in the Paris time zone.

Member

by samara , a year ago

@domenico 

In PostgreSQL, you can get the current year using the date_part function. Here's an example of how to do it:

1
SELECT date_part('year', CURRENT_DATE) as current_year;


This query will return the current year as an integer value. The CURRENT_DATE function is used to get the current date, and the date_part function extracts the year part from the date.