TO_CHAR

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Converts a timestamp or interval value to a formatted text string using a template pattern.

The first argument depends on the overload used:

  • <timestamp>: A value of type timestamp or timestamp with time zone.
  • <interval>: A value of type interval.

The second argument applies to both overloads:

  • <format>: A text template string containing one or more template patterns that define the output format.

Returns text. Returns NULL if either argument is NULL.

In a to_char output template string, recognized patterns are replaced with appropriately formatted data based on the given value. Any text that is not a template pattern is copied verbatim.

PatternDescription
HHHour of day on a 12-hour clock (01–12)
HH12Hour of day on a 12-hour clock (01–12)
HH24Hour of day on a 24-hour clock (00–23)
MIMinutes (00–59)
SSSeconds (00–59)
MSMilliseconds (000–999)
USMicroseconds (000000–999999)
SSSSSeconds elapsed since midnight (0–86399)
AM, am, PM or pmMeridiem indicator without periods (for example, AM or PM)
A.M., a.m., P.M. or p.m.Meridiem indicator with periods (for example, A.M. or P.M.)
Y,YYYFull year (4 or more digits) with a comma separator (for example, 2,024)
YYYYFull year (4 or more digits, for example, 2024)
YYYLast 3 digits of the year
YYLast 2 digits of the year
YLast digit of the year
IYYYISO 8601 week-numbering year, 4 or more digits
IYYLast 3 digits of the ISO 8601 week-numbering year
IYLast 2 digits of the ISO 8601 week-numbering year
ILast digit of the ISO 8601 week-numbering year
BC, bc, AD or adEra indicator without periods (for example, BC or AD)
B.C., b.c., A.D. or a.d.Era indicator with periods (for example, B.C. or A.D.)
MONTHFull month name in upper case, blank-padded to 9 characters (for example, JANUARY)
MonthFull month name with initial capital, blank-padded to 9 characters (for example, January)
monthFull month name in lower case, blank-padded to 9 characters (for example, january)
MONAbbreviated month name in upper case, 3 characters (for example, JAN)
MonAbbreviated month name with initial capital, 3 characters (for example, Jan)
monAbbreviated month name in lower case, 3 characters (for example, jan)
MMMonth number (01–12)
DAYFull day name in upper case, blank-padded to 9 characters (for example, MONDAY)
DayFull day name with initial capital, blank-padded to 9 characters (for example, Monday)
dayFull day name in lower case, blank-padded to 9 characters (for example, monday)
DYAbbreviated day name in upper case, 3 characters (for example, MON)
DyAbbreviated day name with initial capital, 3 characters (for example, Mon)
dyAbbreviated day name in lower case, 3 characters (for example, mon)
DDDDay of the year (001–366)
IDDDDay of the ISO 8601 week-numbering year (001–371; day 1 is Monday of the first ISO week)
DDDay of the month (01–31)
DDay of the week, where Sunday is 1 and Saturday is 7
IDISO 8601 day of the week, where Monday is 1 and Sunday is 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 (01–53); week 1 contains the first Thursday of the year
CCCentury as 2 digits (for example, 21 for the years 2001–2100)
JJulian Day — integer count of days since November 24, 4714 BC at midnight UTC
QQuarter of the year (1–4)
RMMonth in upper case Roman numerals (I–XII, where I = January)
rmMonth in lower case Roman numerals (i–xii, where i = January)
TZTime zone abbreviation in upper case (for example, PST); output only
tzTime zone abbreviation in lower case (for example, pst); output only
TZHTime zone offset hours from UTC (for example, -07)
TZMTime zone offset minutes from UTC (for example, 30)
OFTime zone offset from UTC in hours and minutes (for example, -07:00); output only

Modifiers can be applied to any template pattern to alter its behavior. For example, FMMonth is the Month pattern with the FM modifier.

ModifierDescriptionExample
FM prefixfill mode (suppress leading zeroes and padding blanks)FMMonth
TH suffixupper case ordinal number suffixDDTH, for example, 12TH
th suffixlower case ordinal number suffixDDth, for example, 12th
  • FM suppresses leading zeroes and trailing blanks that are added to make the output of a pattern fixed-width. FM modifies only the next specification, and repeated FM modifiers toggle fill mode on and off.

  • Text in to_char templates is output literally. Put a substring in double quotes to force it to be interpreted as literal text even if it contains template patterns. For example, in '"Hello Year "YYYY', the YYYY is replaced by the year data, but the single Y in Year is not replaced.

  • To include a double quote in the output, precede it with a backslash. For example, '\"YYYY Month\"'. Within a double-quoted string, a backslash causes the next character to be taken literally.

  • to_char can use a mixture of Gregorian and ISO week-numbering date fields, but values such as IYYY-MM-DD yield mixed results near the start of the year.

Mixing ISO 8601 week-numbering fields (IYYY, IW, ID, IDDD) with Gregorian date fields in the same template can produce unexpected results.

  • to_char(..., 'ID') day-of-week numbering matches extract(isodow from ...), but to_char(..., 'D') does not match extract(dow from ...) numbering.

  • to_char(interval) formats HH and HH12 as shown on a 12-hour clock. Zero hours and 36 hours both output as 12. HH24 outputs the full hour value, which can exceed 23 in an interval value.

Format the current timestamp as a day-of-week and time string.

Without fill mode:

Returns a string such as 'Tuesday , 06 05:39:18'.

With fill mode (FM) to suppress padding:

Returns a string such as 'Tuesday, 6 05:39:18'.

Format a 15-hour 2-minute 12-second interval as a 24-hour time string.

Returns '15:02:12'.

Display the day of the month with an ordinal suffix and the full month name.

Returns '5th March 2024'.