Why does MySQL stores date in the object of type "TIME"?

96 Views Asked by At

I've discovered that function YEAR(CURRENT_TIME()) returns an 2023 year instead of NULL as I thought or something like that.

When i made this query

SELECT DAY(CURRENT_TIME()), MONTH(CURRENT_TIME()), YEAR(CURRENT_TIME());

I got the next result

+---------------------+-----------------------+----------------------+
| DAY(CURRENT_TIME()) | MONTH(CURRENT_TIME()) | YEAR(CURRENT_TIME()) |
+---------------------+-----------------------+----------------------+
|                  24 |                    11 |                 2023 |
+---------------------+-----------------------+----------------------+

So why does 'TIME' implicitly stores the date?

1

There are 1 best solutions below

2
On BEST ANSWER

The answer is that mysql performs an implicit time -> datetime conversion when you call the year() function on the returned time as year() cannot operate on time data type.

As mysql manual on date and time data types conversion says:

For conversion of TIME values to other temporal types, the value of CURRENT_DATE() is used for the date part.