Back to Materialize

to_char function

doc/user/content/sql/functions/to_char.md

1237.9 KB
Original Source

to_char converts a timestamp into a string using the specified format.

The format string can be composed of any number of format specifiers, interspersed with regular text. You can place a specifier token inside of double-quotes to emit it literally.

Examples

RFC 2822 format

mzsql
SELECT to_char(TIMESTAMPTZ '2019-11-26 15:56:46 +00:00', 'Dy, Mon DD YYYY HH24:MI:SS +0000') AS formatted
nofmt
             formatted
 ---------------------------------
  Tue, Nov 26 2019 15:56:46 +0000

Additional non-interpreted text

Normally the W in "Welcome" would be converted to the week number, so we must quote it. The "to" doesn't match any format specifiers, so quotes are optional.

mzsql
SELECT to_char(TIMESTAMPTZ '2019-11-26 15:56:46 +00:00', '"Welcome" to Mon, YYYY') AS formatted
nofmt
       formatted
 ----------------------
  Welcome to Nov, 2019

Ordinal modifiers

mzsql
SELECT to_char(TIMESTAMPTZ '2019-11-1 15:56:46 +00:00', 'Dth of Mon') AS formatted
nofmt
  formatted
 ------------
  6th of Nov

Format specifiers

SpecifierDescription
HHhour of day (01-12)
HH12hour of day (01-12)
HH24hour of day (00-23)
MIminute (00-59)
SSsecond (00-59)
MSmillisecond (000-999)
USmicrosecond (000000-999999)
SSSSseconds past midnight (0-86399)
AM/PMuppercase meridiem indicator (without periods)
am/pmlowercase meridiem indicator (without periods)
A.M./P.M.uppercase meridiem indicator (with periods)
a.m./p.m.lowercase meridiem indicator (with periods)
Y,YYYY,YYY year (4 or more digits) with comma
YYYYyear (4 or more digits)
YYYlast 3 digits of year
YYlast 2 digits of year
Ylast digit of year
IYYYISO 8601 week-numbering year (4 or more digits)
IYYlast 3 digits of ISO 8601 week-numbering year
IYlast 2 digits of ISO 8601 week-numbering year
Ilast digit of ISO 8601 week-numbering year
BC/ADuppercase era indicator (without periods)
bc/adlowercase era indicator (without periods)
B.C./A.D.uppercase era indicator (with periods)
b.c./a.d.lowercase era indicator (with periods)
MONTHfull upper case month name (blank-padded to 9 chars)
Monthfull capitalized month name (blank-padded to 9 chars)
monthfull lower case month name (blank-padded to 9 chars)
MONabbreviated upper case month name (3 chars in English, localized lengths vary)
Monabbreviated capitalized month name (3 chars in English, localized lengths vary)
monabbreviated lower case month name (3 chars in English, localized lengths vary)
MMmonth number (01-12)
DAYfull upper case day name (blank-padded to 9 chars)
Dayfull capitalized day name (blank-padded to 9 chars)
dayfull lower case day name (blank-padded to 9 chars)
DYabbreviated upper case day name (3 chars in English, localized lengths vary)
Dyabbreviated capitalized day name (3 chars in English, localized lengths vary)
dyabbreviated lower case day name (3 chars in English, localized lengths vary)
DDDday of year (001-366)
IDDDday of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)
DDday of 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 month (1-5) (the first week starts on the first day of the month)
WWweek number of year (1-53) (the first week starts on the first day of the year)
IWweek number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)
CCcentury (2 digits) (the twenty-first century starts on 2001-01-01)
JJulian Day (days since November 24, 4714 BC at midnight)
Qquarter (ignored by to_date and to_timestamp)
RMmonth in upper case Roman numerals (I-XII; I=January)
rmmonth in lower case Roman numerals (i-xii; i=January)
TZupper case time-zone name
tzlower case time-zone name

Specifier modifiers

ModifierDescriptionExampleWithout ModificationWith Modification
FM prefixfill mode (suppress leading zeroes and padding blanks)FMMonthJuly July
TH/th suffixupper/lower case ordinal number suffixDth11st