Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql query current year

Tags:

sql

postgresql

I need to see only the current year rows from a table.

Would it be possible filter a timestamp column only by current year parameter, is there some function that can return this value?

SELECT * FROM mytable WHERE "MYDATE" LIKE CURRENT_YEAR
like image 921
user2239318 Avatar asked Feb 29 '16 16:02

user2239318


People also ask

How do I get the current year in SQL query?

MySQL YEAR() Function The YEAR() function returns the year part for a given date (a number from 1000 to 9999).

How do I get the current date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.

What is now () in PostgreSQL?

What is PostgreSQL Now Function? The Now() function is used to return the current date and time of the time zone (default or user-defined). Its return type is the timestamp with the time zone.


2 Answers

In PostgreSQL you can use this:

SELECT * FROM mytable WHERE date_part('year', mydate) = date_part('year', CURRENT_DATE);

The date_part function is available in all PostgreSQL releases from current down to 7.1 (at least).

like image 190
MyBrainHurts Avatar answered Oct 02 '22 14:10

MyBrainHurts


This is pretty old, but now you can do:

SELECT date_part('year', now()); -> 2020

In the place of now you can pass any timestamp. More on Documentation

like image 35
Kevin Alemán Avatar answered Oct 02 '22 14:10

Kevin Alemán