IT demon
IT demon

Reputation: 67

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

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?

Upvotes: -1

Views: 119

Answers (1)

Shadow
Shadow

Reputation: 34231

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.

Upvotes: 1

Related Questions