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
MySQL YEAR() Function The YEAR() function returns the year part for a given date (a number from 1000 to 9999).
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 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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With