DATE
DATE is a date data type that stores three parts: year, month, and day, without time or timezone information. It is suitable for storing date data unrelated to time, such as birthdays and event dates.
Syntax
Parameters
| Parameter | Format | Description |
|---|---|---|
yyyy | Four digits | Year, e.g. 2024 |
MM | Two digits | Month, range 01 to 12 |
dd | Two digits | Day, range 01 to 31 (depending on the month) |
Examples
-
Use a DATE literal:
Returns:
2024-01-15 -
Convert a string to DATE:
Returns:
2024-01-15 -
Calculate the number of days between two dates:
Returns:
17 -
Date addition/subtraction (using INTERVAL):
Returns:
2024-02-14 -
Convert DATE to TIMESTAMP:
Returns:
2024-01-14T16:00:00.000Z(converted to UTC time; the timezone offset depends on the session timezone setting) -
Invalid date conversion returns NULL:
Returns:
NULL -
NULL value handling:
Returns:
NULL
Notes
DATEis a data type, not a function. Date literal syntax isDATE'yyyy-MM-dd'; do not writeDATE('2024-01-15').- When the month or day is outside the valid range, CAST conversion returns NULL without raising an error.
- DATE does not include time information; to store time, use the
TIMESTAMPtype. - When converting DATE to TIMESTAMP, the time part defaults to
00:00:00and is converted according to the session timezone. - Date comparisons can use
<,>,=and other operators directly without additional conversion. - Avoid using non-existent dates (such as
2024-02-30); such values return NULL after conversion.
