Tushar Kulkarni
Tushar Kulkarni

Reputation: 323

Compare multiple column with single value in postgresql

Below is my table structure

ID    DATE1           DATE2         DATE3
1     2018-05-01      2018-05-01    2018-11-11
2     2018-05-01      2018-10-01    2018-05-01
3     2018-05-01      2018-05-01    2018-05-01

Here actually I wanted to select record where any one of the three dates is greater than now()

like - (DATE1, DATE2, DATE3) > NOW()::DATE

Can anyone let me know that how can I achieve this in where clause instead of writing like

SELECT *
FROM table1
where DATE1 > NOW()::DATE
  AND DATE2 > NOW()::DATE
  AND DATE3 > NOW::DATE

Actually, I am going to compare all dates column with the single value, so just wanted to know is there any other efficient way that I can use instead of adding the condition for each column.

Upvotes: 0

Views: 1391

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271023

I would use GREATEST():

SELECT t1.*
FROM table1 t1
WHERE GREATEST(date1, date2, date) > CURRENT_DATE;

I think CURRENT_DATE makes more sense than converting NOW().

Upvotes: 2

Related Questions