Converts a string into a DATE or DATETIME value using a specified format pattern.
Syntax
DATETIME|DATE TO_DATE(STRING <date>[, STRING <format>])Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
date | Yes | STRING | The string to convert. BIGINT, DOUBLE, DECIMAL, and DATETIME values are implicitly converted to STRING before processing. ISO 8601 format strings are also supported. |
format | No | STRING (constant) | The format pattern to apply. Does not support date extension formats. Characters not recognized as format specifiers are ignored during parsing. Must contain yyyy, and yyyy can appear only once. If this condition is not met, the function returns NULL. |
Format specifiers
| Specifier | Meaning | Example value |
|---|---|---|
yyyy | Four-digit year | 2025 |
mm | Two-digit month | 01 |
dd | Two-digit day | 29 |
hh | Hour (24-hour clock) | 15 |
mi | Two-digit minute | 13 |
ss | Two-digit second | 12 |
ff3 | Three-digit millisecond | 345 |
Return value
| Condition | Return type | Format |
|---|---|---|
Called without format | DATE | yyyy-mm-dd |
Called with format | DATETIME | yyyy-mm-dd hh:mi:ss |
date or format is NULL | NULL | — |
Examples
Basic usage (no format)
-- Returns 2025-01-29
SELECT TO_DATE('2025-01-29');Custom format strings
All examples below use the format parameter and return a DATETIME value.
-- Returns 2025-01-27 00:00:00
SELECT TO_DATE('Alibaba2025-01*27', 'Alibabayyyy-mm*dd');
-- Returns 2025-01-12 00:00:00
SELECT TO_DATE('20250112', 'yyyymmdd');
-- Returns 2025-01-28 12:12:00
SELECT TO_DATE('202501281212', 'yyyymmddhhmi');
-- Returns 2025-10-30 15:13:12
SELECT TO_DATE('20251030 15-13-12.345', 'yyyymmdd hh-mi-ss.ff3');ISO 8601 format
-- Returns 2025-09-24 13:39:34
SELECT TO_DATE('2025-09-24T13:39:34.119Z', 'yyyy-MM-ddThh:mi:ss.ff3Z');NULL inputs
-- Returns NULL
SELECT TO_DATE(NULL, 'yyyymmdd hh-mi-ss.ff3');
-- Returns NULL
SELECT TO_DATE('20251030 15-13-12.345', NULL);Invalid inputs
-- Returns NULL. '2025112' does not match 'yyyymmdd' (day is missing a digit; use '20250112')
SELECT TO_DATE('2025112', 'yyyymmdd');
-- Returns NULL. Day value '3' is not two digits (use 'Alibaba2025-12*03')
SELECT TO_DATE('Alibaba2025-12*3', 'Alibabayyyy-mm*dd');
-- Returns NULL. '2025-24-01' cannot be converted to a standard date value. It should be '2025-01-24'.
SELECT TO_DATE('2025-24-01', 'yyyy');Table data example
Create the example table and insert data.
CREATE TABLE IF NOT EXISTS mf_date_fun_t( id INT, date1 DATE, datetime1 DATETIME, timestamp1 TIMESTAMP, date2 DATE, datetime2 DATETIME, timestamp2 TIMESTAMP, date3 STRING, date4 BIGINT); INSERT INTO mf_date_fun_t VALUES (1,DATE'2021-11-29',DATETIME'2021-11-29 00:01:00',TIMESTAMP'2021-01-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-20',123456780), (2,DATE'2021-11-28',DATETIME'2021-11-28 00:02:00',TIMESTAMP'2021-02-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-21',123456781), (3,DATE'2021-11-27',DATETIME'2021-11-27 00:03:00',TIMESTAMP'2021-03-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-22',123456782), (4,DATE'2021-11-26',DATETIME'2021-11-26 00:04:00',TIMESTAMP'2021-04-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-23',123456783), (5,DATE'2021-11-25',DATETIME'2021-11-25 00:05:00',TIMESTAMP'2021-05-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-24',123456784), (6,DATE'2021-11-24',DATETIME'2021-11-24 00:06:00',TIMESTAMP'2021-06-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-25',123456785), (7,DATE'2021-11-23',DATETIME'2021-11-23 00:07:00',TIMESTAMP'2021-07-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-26',123456786), (8,DATE'2021-11-22',DATETIME'2021-11-22 00:08:00',TIMESTAMP'2021-08-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-27',123456787), (9,DATE'2021-11-21',DATETIME'2021-11-21 00:09:00',TIMESTAMP'2021-09-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-28',123456788), (10,DATE'2021-11-20',DATETIME'2021-11-20 00:10:00',TIMESTAMP'2021-10-11 00:00:00.123456789',DATE'2021-10-29',DATETIME'2021-10-29 00:00:00',TIMESTAMP'2021-10-11 00:00:00.123456789','2021-11-29',123456789);Convert the STRING column
date3to DATETIME.SELECT date3, TO_DATE(date3, 'yyyy-mm-dd') AS date3_to_date FROM mf_date_fun_t;Result:
+------------+---------------------+ | date3 | date3_to_date | +------------+---------------------+ | 2021-11-20 | 2021-11-20 00:00:00 | | 2021-11-21 | 2021-11-21 00:00:00 | | 2021-11-22 | 2021-11-22 00:00:00 | | 2021-11-23 | 2021-11-23 00:00:00 | | 2021-11-24 | 2021-11-24 00:00:00 | | 2021-11-25 | 2021-11-25 00:00:00 | | 2021-11-26 | 2021-11-26 00:00:00 | | 2021-11-27 | 2021-11-27 00:00:00 | | 2021-11-28 | 2021-11-28 00:00:00 | | 2021-11-29 | 2021-11-29 00:00:00 | +------------+---------------------+
Related functions
TO_DATE is part of the date function suite. For more information on date calculation and conversion functions, see Date functions.