Last active
December 18, 2015 00:39
-
-
Save alancoleman/5698485 to your computer and use it in GitHub Desktop.
There are three date columns in the table, each can contain any date or be null. This query will return all records between a date range when at least one of the date columns has a value, taking the highest or lowest value date from the row.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT date1, date2, date3 | |
FROM dates AS d | |
WHERE GREATEST(IFNULL(d.date1,MAKEDATE(2099,365)), | |
IFNULL(d.date2,MAKEDATE(2099,365)), | |
IFNULL(d.date3,MAKEDATE(2099,365))) <= '2013-06-03 16:08:46' | |
AND | |
LEAST(IFNULL(d.date1,MAKEDATE(1900,365)), | |
IFNULL(d.date2,MAKEDATE(1900,365)), | |
IFNULL(d.date3,MAKEDATE(1900,365))) >= '2008-04-01 16:08:46' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment