DATE

DATE type is used to represent a date, including three parts: year, month, and day, but does not include time zone information. This type is very useful when dealing with data that is not related to time, such as recording a user's birth date or the date an event occurred.

Syntax

DATE [year]-[month]-[day]
  • year: Year, formatted as a four-digit number, such as 2023.
  • month: Month, formatted as a two-digit number, ranging from 01 to 12, such as 03.
  • day: Day, formatted as a two-digit number, ranging from 01 to 31 depending on the month, such as 15.

Example

  1. Insert the current date:
    SELECT DATE('2023-03-15');
2. Convert from string to date:
SELECT DATE('2022-12-31');
3. Get the date part from the current time:
SELECT DATE(TIMESTAMP "2023-03-15 10:30:00");
  1. Calculate the difference between two dates:
    SELECT DATE('2023-04-01') - DATE('2023-03-15') AS DateDifference;
  2. Convert the string to a date and compare:
    SELECT '2023-03-15' = DATE('2023-03-15') AS IsEqual;

Notes

  • When the input month or date is out of the actual range, an error will be returned.
  • When performing date calculations, pay attention to the legality of the result to avoid non-existent dates (e.g., 2023-02-30).