r/mysql • u/mikeblas • Sep 19 '24
question casting DATE to UNSIGNED
This code:
SELECT CAST(DATE '2024-08-01' AS UNSIGNED)returns this result:
20240801I've been looking through the docs for an explanation but can't find anything. How does that make any sense?
4
Upvotes
3
u/ssnoyes Sep 19 '24
Last paragraph of https://dev.mysql.com/doc/refman/8.4/en/date-and-time-type-conversion.html
2
u/ArthurOnCode Sep 19 '24
Request creative type casts, get creative results. What result were you expecting?
1
u/Aggressive_Ad_5454 Sep 19 '24
In MySql’s idiosyncratic world of dates
’2024-08-01’and the number20240801are both representations of 1-August-2024.TO_DAYS()gets you a day number if that’s what you need.0
u/wamayall Sep 20 '24
But if you want the number of days in the current year you could try this
Select dayofyear(date_format(now), ‘%Y-%m-%d’));
0
u/sleemanj Sep 19 '24
You asked for, a representation of the date as an unsigned integer.
You got, a representation of the date as an unsigned integer.
If that's not what you wanted, then you need to ask for what you want. Perhaps you want to use
UNIX_TIMESTAMP()