PostgreSQL to_date() Function
The PostgreSQL to_date() function converts the specified string to a date value according to the given format.
to_date() Syntax
This is the syntax of the PostgreSQL to_date () function:
to_date(str TEXT, format TEXT) -> TIMESTAMP WITH TIME ZONE
Parameters
str-
Required. The string to convert.
format-
Required. The format in which to convert this string. You can use the template patterns in the following table to construct format strings.
Pattern Description HHHour of the day (01–12) HH12Hour of the day (01–12) HH24Hour of the day (00–23) MIMinutes (00–59) SSseconds (00–59) MSmilliseconds (000–999) USMicroseconds (000000–999999) FF1tenths of a second (0–9) FF2hundredths of a second (00–99) FF3milliseconds (000–999) FF4Tenth of a millisecond (0000–9999) FF5hundredths of a millisecond (00000–99999) FF6Microseconds (000000–999999) SSSS,SSSSSSeconds after midnight (0–86399) AM,am,PM,pmAM, PM (no period) A.M.,a.m.,P.M.,p.m.AM, PM (with periods) Y,YYYYear (4 or more digits) with commas YYYYyear (4 or more digits) YYYLast 3 digits of year YYLast 2 digits of year Ylast digit of year IYYYISO 8601 week number year (4 or more digits) IYYLast 3 digits of ISO 8601 week number year IYLast 2 digits of ISO 8601 week number year IISO 8601 week number last digit of year BC,bc,AD,adBC, AD (no period) B.C.,b.c.,A.D.,a.d.BC, AD (with periods) MONTHAll-caps month name (blank padded to 9 characters) MonthAll-caps month name (blank padded to 9 characters) monthAll lowercase month name (blank padded to 9 characters) MONAbbreviated uppercase month name (3 characters in English, localized lengths vary) MonAbbreviated uppercase month name (3 characters in English, localized lengths vary) monAbbreviated lowercase month name (3 characters in English, localized lengths vary) MMNumber of months (01–12) DAYDate name in all caps (blank padded to 9 characters) DayDate name in all caps (blank padded to 9 characters) dayAll lowercase date name (9 characters padded with blanks) DYAbbreviated uppercase date name (3 English characters, localized lengths vary) DyAbbreviated uppercase date name (3 English characters, localized lengths vary) dyAbbreviated lowercase date name (3 English characters, localized lengths vary) DDDday of the year (001–366) IDDDISO 8601 week number day of the year (001–371; the 1st day of the year is the Monday of the first ISO week) DDday of the month (01–31) DDay of the week, Sunday ( 1) to Saturday (7)IDISO 8601 Day of the week, Monday ( 1) to Sunday (7)WWeek of the month (1–5) (the first week starts on the first day of the month) WWWeek number of the year (1–53) (the first week starts on the first day of the year) IWISO 8601 week number Week number of the year (01–53; the first Thursday of the year is in week 1) CCCentury (2 digits) (21st century started on 2001-01-01) JJulian calendar date (integer days since local midnight on November 24, 4714 BC) Qquarter RMMonth in uppercase Roman numerals (I to XII; I is January) rmMonth in lowercase Roman numerals (i to xii; i is January) TZHtime zone hours TZMtime zone minutes You can use the modifiers in the following table with date/time template patterns.
Modifier Description FMprefixfill mode (suppress leading zeroes and padding blanks) THsuffixupper case ordinal number suffix thsuffixlower case ordinal number suffix FXprefixfixed format global option (see usage notes) TMprefixtranslation mode (use localized day and month names based on lc_time) SPsuffixspell mode (not implemented)
Return value
The PostgreSQL to_date() function returns a date value converted from the specified string according to the specified format.
to_date() Examples
This example shows how to use the PostgreSQL to_date() function to convert a specified time string to a date value according to the specified format.
SELECT to_date('2022-05-17', 'YYYY-MM-DD');
to_date
------------
2022-05-17