TO_DATE
Converts a string value to a DATE type.
Behavior type
StableSyntax
TO_DATE ( expression , pattern )
Parameters
expression- Specifies the string value to convert, either
CHARorVARCHAR. pattern- A
CHARorVARCHARthat specifies an output pattern string. See:
Input value considerations
TO_DATE requires a CHAR or VARCHAR expression. For other input types, use
TO_CHAR to perform an explicit cast to a CHAR or VARCHAR before using this function.
Notes
- To use a double quote character in the output, precede it with a double backslash. This is necessary because the backslash already has a special meaning in a string constant. For example:
'\\"YYYY Month\\"'
-
TO_TIMESTAMP,TO_TIMESTAMP_TZ, andTO_DATEskip multiple blank spaces in the input string if the FX option is not used. FX must be specified as the first item in the template. For example:-
TO_TIMESTAMP('2000 JUN', 'YYYY MON')is correct. -
TO_TIMESTAMP('2000 JUN', 'FXYYYY MON')returns an error, becauseTO_TIMESTAMPexpects one space only.
-
-
The
YYYYconversion from string toTIMESTAMPorDATEhas a restriction if you use a year with more than four digits. You must use a non-digit character or template afterYYYY, otherwise the year is always interpreted as four digits. For example, given the following arguments,TO_DATEinterprets the five-digit year 20000 as a four-digit year:=> SELECT TO_DATE('200001131','YYYYMMDD'); TO_DATE ------------ 2000-01-13 (1 row)Instead, use a non-digit separator after the year. For example:
=> SELECT TO_DATE('20000-1131', 'YYYY-MMDD'); TO_DATE ------------- 20000-12-01 (1 row) -
In conversions from string to
TIMESTAMPorDATE, the CC field is ignored if there is a YYY, YYYY or Y,YYY field. If CC is used with YY or Y, then the year is computed as (CC–1)*100+YY.
Examples
=> SELECT TO_DATE('13 Feb 2000', 'DD Mon YYYY');
to_date
------------
2000-02-13
(1 row)