How to get the current year in PostgreSQL?

Member

by domenico , in category: PHP Databases , 8 months ago

How to get the current year in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 3 months 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.