I know that the code is incorrect and that the ADDDATE function should be used, but I’m trying to find out if a specific behaviour is caused by this bug.
So, does anyone know what exactly happens if I have the statement
SELECT * FROM MyTable WHERE TheTimestamp > (NOW()-86400);
when TheTimestamp is of the datetime data type?
2
Answers
It’s not a bug. You’re just expecting a datetime cast to a number to be something it’s not.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html says:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-types.html says:
So using
NOW()
in an arithmetic expression converts it to a number:You can see this converts ‘2022-10-19 14:13:14’ (the date and time I test this query) into an integer simply by removing the punctuation and whitespace. This isn’t a number you can add or subtract with, because the values 60-99 aren’t used for seconds or minutes, and likewise the values 24-99 for hours, 32-99 for days, 13-99 for months, etc.
The expression you showed doesn’t produce a number that is valid as a timestamp:
To do arithmetic on the datetime, you should either convert to a integer measure of seconds, then use that value in integer expressions:
Or else use MySQL’s support for temporal
INTERVAL
expressions. See https://dev.mysql.com/doc/refman/8.0/en/expressions.html#temporal-intervalsDATETIME and TIMESTAMP values are implicitly integer values of 5 and 4 bytes in length respectively (with optional additional bytes for fractional seconds precision). Mathematical operators, as well as conditional operators, will work accordingly. However, this will not account for all the implicit conversions like when you use
ADDTIME()
.This operation specifically looks for any records created later than 1 day ago, since the DATETIME value has to be greater than (later in time) the current time – 86400 seconds (1 day/24 hours) ago.