Aman Sharma
Aman Sharma

Reputation: 93

Filter a table column using year in Postgresql

I have a table in which one of the columns is m_date with type timestamp with timezone.

I want to write a query to filter the rows where the year is 2020.

Upvotes: 0

Views: 421

Answers (1)

user330315
user330315

Reputation:

One way is to use a range query:

select *
from the_table
where m_date >= date '2020-01-01'
  and m_date < date '2021-01-01';

This can use an index in m_date.

Alternatively (but slower!)

select *
from the_table
where extract(year from m_date) = 2020

Upvotes: 1

Related Questions