to_char function

to_char function converts a number or date to a string. The syntax for the to_char function is:

to_char( value, [ format_mask ], [ nls_language ] )

ParameterDescription
valuea number or date that will be converted to a string.
format_maskoptional. format to be used to convert value to a string.
nls_languageoptional. the nls language used to convert value to a string.

The structure format depends on whether x is a number or date.

The number format string:

ParameterDescription
9Returns digits in specified positions, with a leading negative sign if negative.
00999: Returns a number with leading zeros. 9990: Returns a number with trailing zeros.
.Returns a decimal point in the specified position.
,Returns a comma in the specified position.
$Returns a leading dollar sign.
BIf the integer part of a fixed point number is zero, returns spaces for the zeros.
CReturns the ISO currency symbol. The symbol is from the NLS_ISO_CURRENCY database parameter.
DReturns the decimal point symbol. The symbol is from the NLS_NUMERIC_CHARACTER database parameter. The default is a period character.
EEEEReturns the number in the scientific notation.
FMRemoves leading and trailing spaces from the number.
GReturns the group separator symbol. The symbol is from the NLS_NUMERIC_CHARACTER database parameter.
LReturns the local currency symbol. The symbol is from the NLS_CURRENCY database parameter.
MIReturns a negative number with a trailing minus sign. Returns a positive number with a trailing space.
PR Returns a negative number in angle brackets(<>). Returns a positive number with leading and trailing spaces.
RNReturns the number as Roman numerals in uppercase, and the number must be an integer between 1 and 3999.
rnReturns the number as Roman numerals in lowercase, and the number must be an integer between 1 and 3999.
SS999 returns a negative number with a leading negative sign; returns a positive number with a leading positive sign. 999S returns a negative number with a trailing negative sign; returns a positive number with a trailing positive sign.
TMReturns the number using the minimum number of characters. The default is TM9.
UReturns the dual currency symbol in the specified position. The symbol is set by NLS_DUAL_CURRENCY database parameter.
VReturns the number multiplied by 10x where x is the number of 9 characters after the V. If necessary, the number is rounded.
XReturns the number in hexadecimal. If the number is not an integer, the number is rounded to an integer.

TO_CHAR()Output
TO_CHAR(12345.67, '99999.99')12345.67
TO_CHAR(12345.67, '99,999.99')12,345.67
TO_CHAR(-12345.67, '99,999.99')-12,345.67
TO_CHAR(12345.67, '099,999.99')012,345.67
TO_CHAR(12345.67, '99,999.9900')12,345.6700
TO_CHAR(12345.67, '$99,999.99')$12,345.67
TO_CHAR(0.67, 'B9.99').67
TO_CHAR(12345.67, 'C99,999.99')USD12,345.67
TO_CHAR(12345.67, '99999D99')12345.67
TO_CHAR(12345.67, '99999.99EEEE')1.23E+04
TO_CHAR(0012345.6700, 'FM99999.99')12345.67
TO_CHAR(12345.67, '99999G99')123,46
TO_CHAR(12345.67, 'L99,999.99')$12,345.67
TO_CHAR(-12345.67, '99,999.99MI')12,345.67
TO_CHAR(-12345.67, '99,999.99PR')12,345.67
TO_CHAR(2007, 'RN')MMVII
TO_CHAR(12345.67, 'TM')12345.67
TO_CHAR(12345.67, 'U99,999.99')$12,345.67
TO_CHAR(12345.67, '99999V99')1234567

Convert numbers to string and do the format:


SQL> SELECT TO_CHAR(12345.67) FROM dual;

TO_CHAR(
--------
12345.67

SQL> select to_char(1234.56, '9999.9') from dual;

TO_CHAR
-------
 1234.6

SQL> select to_char(1234.56, '9,999.99') from dual;

TO_CHAR(1
---------
 1,234.56

SQL> SELECT TO_CHAR(12345.67, '99,999.99') FROM dual;

TO_CHAR(12
----------
 12,345.67

SQL> select to_char(1234.56, '$9,999.00') from dual;

TO_CHAR(12
----------
 $1,234.56

SQL> select to_char(123, '000099') from dual;

TO_CHAR
-------
 000123

SQL>

TO_CHAR() returns # if the number has too many digits for the format. For example:


SQL> SELECT TO_CHAR(12345678.90, '99,999.99') FROM dual;

TO_CHAR(12
----------
##########

SQL>

Format parameters for converting a date value to string.

ParameterDescription
CCTwo-digit century.
SCCTwo-digit century with a negative sign (-) for B.C.
QOne-digit quarter of the year. (1, 2, 3, 4; JAN-MAR = 1)
YYYYAll 4 digits of the year.
IYYYAll 4 digits of the ISO year.
RRRRAll 4 digits of the rounded year (governed by the present year).
SYYYYAll 4 digits of the year with a negative sign (-) for B.C.
Y,YYYAll 4 digits of the year, with a comma after the first digit.
YYYLast 3 digits of the year.
IYYLast 3 digits of the ISO year.
YYLast 2 digits of the year.
IYLast 2 digits of the ISO year.
RRLast two digits of the rounded year, which depend on the present year.
YLast digit of the year.
ILast digit of the ISO year.
YEARName of the year in uppercase.
YearName of the year with the first letter in uppercase.
MMTwo-digit month of the year. (01-12; JAN = 01)
MONTHFull name of the month in uppercase, right-padded with spaces to a total length of nine characters.
MonthFull name of the month with first letter in uppercase, right-padded with spaces to a total length of nine characters.
MONFirst three letters of the name of the month in uppercase.
MonFirst three letters of the name of the month with the first letter in uppercase.
RMRoman numeral month. (I-XII; JAN = I)
WWTwo-digit week of the year. (1-53)
IWTwo-digit ISO week of the year.(1-52 or 1-53)
WOne-digit week of the month.(1-5)
DDDThree-digit day of the year. (1-366)
DDTwo-digit day of the month.(1-31)
DOne-digit day of the week.(1-7)
DAYFull name of the day in uppercase.
DayFull name of the day with the first letter in uppercase.
DYFirst three letters of the name of the day in uppercase.
DyFirst three letters of the name of the day with the first letter in uppercase.
JJulian day-the number of days that have passed since January 1, 4713 B.C.
HH24Two-digit hour in 24-hour format. (0-23)
HH12Hour of day.(1-12)
HHTwo-digit hour in 12-hour format.(1-12)
MITwo-digit minute. (0-59)
SSTwo-digit second.(0-59)
FF[1..9]Fractional seconds.
SSSSSNumber of seconds past 12 A.M. (0-86399)
MSMillisecond (millionths of a second).
CSCentisecond (hundredths of a second).
-/,.;:"text"Characters that allow you to separate the aspects of a date and time.
AM or PM A.M. or P.M. AD or BCAM or PM A.M. or P.M. AD or BC
SPNumber is spelled out
SPTHCombination of SP and TH
EEFull era name
EAbbreviated era name
TZHTime zone hour.
TZMTime zone minute.
TZRTime zone region.
TZDTime zone with daylight savings information.

The following table illustrates some TO_CHAR date formatting:

FormatOutput
dd Month yyyy05 March 2012
dd month YY05 march 12
dd Mon05 Mar
dd RM yyyy05 III 2012
Day Mon yyyySunday Mar 2012
Day fmMonth dd, yyyySunday March 5, 2012
Mon ddsp yyyyMar five 2012
ddMon yy hh24:mi:ss05Mar 12 00:00:00
MONTH DD, YYYYFEBRUARY 05, 2012
MM/DD/YYYY02/05/2012
MM-DD-YYYY02-05-2012
DD/MM/YYYY05/02/2012
DAY MON, YY ADMONDAY FEB, 12 AD
DDSPTH "of" MONTH, YEAR A.D.FIFTH of FEBRUARY, TWENTY TWELVE A.D.
CC, SCC20, 20
Q1
YYYY2012
IYYY2012
RRRR2012
SYYYY2012
Y,YYY2,012
YYY012
IYY012
YY12
IY12
RR12
Y2
I2
YEARTWENTY TWELVE
YearTwenty Twelve
MM02
MONTHFEBRUARY
MonthFebruary
MONFEB
MonFeb
RMII
WW02
IW02
W2
DDD035
DD06
DAYMONDAY,
DayMonday
DYMON
DyMon
J2439999
ddTH05th
DDTH05TH
ddSPfive
DDSPFIVE
DDSPTHFIFTH
HH24:MI:SS19:12:36
HH.MI.SS AM7.12.36 PM

SQL> select to_char(sysdate, 'Month DD, YYYY') FROM dual;

TO_CHAR(SYSDATE,'M
------------------
July      09, 2012

SQL> select to_char(sysdate, 'yyyy/mm/dd')  FROM dual;

TO_CHAR(SY
----------
2012/07/09

SQL>

format_mask beginning with "FM" means that zeros and blanks are suppressed.


SQL> select to_char(sysdate, 'FMMonth DD, YYYY') FROM dual;

TO_CHAR(SYSDATE,'F
------------------
July 9, 2012

SQL> select to_char(sysdate, 'Month DD, YYYY') FROM dual;

TO_CHAR(SYSDATE,'M
------------------
July      09, 2012

SQL>

Adding th to the day and month:


SQL> select to_char(sysdate, 'Month DD, YYYY')  FROM dual;

TO_CHAR(SYSDATE,'M
------------------
July      03, 2011

SQL> select to_char(sysdate, 'MON DDth, YYYY')  FROM dual;

TO_CHAR(SYSDAT
--------------
JUL 03RD, 2011

SQL> select to_char(sysdate, 'Mon ddth, YYYY')  FROM dual;

TO_CHAR(SYSDAT
--------------
Jul 03rd, 2011

SQL> SELECT TO_CHAR(SYSDATE, 'MONTH DD, YYYY, HH24:MI:SS') FROM dual;

TO_CHAR(SYSDATE,'MONTHDD,YYY
----------------------------
JULY      03, 2011, 12:59:20

SQL>
Home »
Oracle »
Conversion Functions » 

to_char:
  1. to_char function
  2. Format salary with to_char
  3. Order by the numeric value of days using to_char
  4. to_char with time format
Related: