How to filter two years ago from current date in postgresql?

by edmond_brakus , in category: MySQL , 8 months ago

How to filter two years ago from current date in postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by aubrey , 8 months ago

@edmond_brakus 

You can use the date_trunc function along with the interval keyword to filter data from two years ago in PostgreSQL.


Here's an example query:

1
2
3
4
SELECT *
FROM your_table
WHERE date_column >= date_trunc('year', current_date - interval '2 years')
AND date_column < date_trunc('year', current_date - interval '1 year')


In this query, date_column is the column in your table that contains the date you want to filter on. The date_trunc function is used to get the start of the year for the current date minus 2 years and the start of the year for the current date minus 1 year, effectively filtering data from two years ago.

Related Threads:

How to get data between two date range in postgresql?
How to compare date values (xsd:date) with years in sparql?
How to join two tables and group items based on latest date on postgresql?
How to filter table with timestamp column in postgresql?
How to filter with two or more combinations in laravel?
How to query current date in mysql?