SQL DATES is a catch-all term that covers the DATE data type, date literals, and built-in operators that work exclusively with calendar dates (year-month-day) without a time-of-day component. A DATE column is stored internally as a three-part value (year, month, day). Most dialects follow the Gregorian calendar starting from 0001-01-01, but minimum and maximum ranges differ.Key behaviors- Precision: DAY is the smallest unit. Time and timezone information are never stored.- Comparisons: DATE values compare chronologically using standard operators (=, <, >, BETWEEN).- Arithmetic: Adding or subtracting integer days or INTERVALs shifts the date while respecting leap years.- Casting and literals: ISO-8601 format (YYYY-MM-DD) is portable. Standard SQL literal syntax is DATE '2024-06-14'.- Functions: CURRENT_DATE, DATE_ADD, DATE_SUB, EXTRACT(YEAR FROM date_val), and TRUNC are commonly supported.Caveats- Implicit time: Casting a DATE to TIMESTAMP usually sets the time portion to midnight (00:00:00).- Timezone shifts: Converting a TIMESTAMP to DATE first converts to session timezone, then truncates the time.- Range limits: MySQL supports 1000-01-01 to 9999-12-31, PostgreSQL -4713-01-01 to 5874897-12-31.- Non-deterministic functions: CURRENT_DATE depends on the session timezone and clock.
DATE data type, DATETIME, TIMESTAMP, INTERVAL, CURRENT_DATE, CAST, EXTRACT
SQL:1992 standard added the DATE data type
Ranges vary by dialect: PostgreSQL supports dates to year 5874897, MySQL to 9999, SQL Server to 9999, SQLite to 5352 when stored as Julian day numbers.
Use an explicit parse or cast with a format mask: in PostgreSQL, `TO_DATE('14/06/2024', 'DD/MM/YYYY')`; in Oracle, `TO_DATE('14/06/2024','DD/MM/YYYY')`.
PostgreSQL: `AGE(CURRENT_DATE, birthday)`.MySQL: `TIMESTAMPDIFF(YEAR, birthday, CURDATE())`.SQL Server: `DATEDIFF(year, birthday, GETDATE()) - CASE WHEN FORMAT(birthday,'MMdd') > FORMAT(GETDATE(),'MMdd') THEN 1 ELSE 0 END`.
Yes. DATE columns are indexable in all major databases and often used in range queries for partition pruning and efficient lookups.