TO_DATE()
Parses a string into a DATE using a format pattern.
Description
TO_DATE converts a text value into a real DATE by interpreting it according
to a format pattern. It's essential when your dates arrive as strings (from a
file or API) and you need to do date math or comparisons on them.
Syntax
TO_DATE('2023-01-15', 'YYYY-MM-DD')Parameters
| Name | Description | Optional |
|---|---|---|
| string | The text to parse. | No |
| format | The pattern matching the input layout. | No |
Return Type
Returns a DATE.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
TO_DATE vs CAST
CAST('2023-01-15' AS DATE) works for ISO formats; TO_DATE handles
arbitrary layouts via a pattern.
Common Mistakes
- Pattern mismatch. If the format doesn't match the string, parsing fails or returns wrong values — align them exactly.
- Format dialect. Pattern tokens vary by engine; check your database's docs.
- Time-of-day input.
TO_DATEdrops any time component; useTO_TIMESTAMPif you need it.
Related Functions
See also: TO_CHAR, CAST, DATE_TRUNC.