0

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.

3
  • I assume the NOW value will only be evaluated once for each query anyway. Commented Sep 7, 2018 at 12:36
  • NOW()::DATE can be simplified to current_date Commented Sep 7, 2018 at 12:37
  • You say "where any one of the three dates is greater than now()", but then you test all dates > now(). Do you mean any or all? Commented Sep 7, 2018 at 12:39

1 Answer 1

2

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().

Sign up to request clarification or add additional context in comments.

2 Comments

@Gordan, Thanks for the answer. Just let me know one more thing, that how we can compare it with NULL.
@TusharKulkarni, Start with adding a few null values to the sample table data, and adjust the expected result accordningly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.