Sarah
Sarah

Reputation: 15

How to retrieve only Thursday dates by passing any date in a week

I need to get only Thursday dates when I pass any date in that week using built-in date functions in postgres. How?

Upvotes: 0

Views: 513

Answers (2)

clamp
clamp

Reputation: 3262

Do you want to get the date of the same weeks thursday returned? That would be:

SELECT date_trunc('week','2020-02-15'::date) + interval '4 days'

Upvotes: 2

Lupf
Lupf

Reputation: 168

You could use the EXTRACT( dow FROM date) function to get what day of the week a given date is and test if the result is a 4, which should be a thursday.

SELECT 
  days :: DATE,
  to_char(days,'Day'),
  EXTRACT(dow FROM days) as day_of_week
FROM generate_series ('2019-02-01', '2019-02-28', '1 day' :: interval ) AS days

https://www.postgresql.org/docs/9.0/functions-datetime.html

Upvotes: 0

Related Questions