I have a MySQL (v 5, MyISAM) query that returns different rows depending on date string format.
(1) IFNULL(date1, ADDDATE('2008/10/31 23:59:59',INTERVAL 1 DAY)) > '2008-10-31 23:59:59' (2) IFNULL(date1, ADDDATE('2008/10/31 23:59:59',INTERVAL 1 DAY)) > '2008/10/31 23:59:59' (3) date1 > '2008-10-31 23:59:59' (4) date1 > '2008/10/31 23:59:59'
‘/’ vs ‘-‘ on RHS of ‘>’ comparisson operator.
(1) 75,098 rows *expected* (2) 0 rows *DIFFERENCE* (3) 199 rows *simple case as expected* (4) 199 rows *simple case as expected*
Question – Why ?
Short answer: Use
CAST(... AS DATE)Long answer:
From MySQL DATE type:
In your examples 1 and 2 you’re using ADDDATE… this will always return a date with a ‘-‘ separator, no matter what the input format:
You’re also using IFNULL, which loses the type information, so when you compare this it’s comparing as strings.
What you can do is cast this back to a date: