Sunday, April 10, 2011

to_char and to_date function in oracle

Source : http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html

When a DATE value is displayed, Oracle must first convert that value from the special internal format to a printable string. The conversion is done by a function TO_CHAR, according to a DATE format. Oracle's default format for DATE is "DD-MON-YY". Therefore, when you issue the query
select b from x;

you will see something like:
B
---------
01-APR-98


Whenever a DATE value is displayed, Oracle will call TO_CHAR automatically with the default DATE format. However, you may override the default behavior by calling TO_CHAR explicitly with your own DATE format. For example,
SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b
FROM x;


returns the result:
B
---------------------------------------------------------------------------
1998/04/01
The general usage of TO_CHAR is:
TO_CHAR(, '')


where the  string can be formed from over 40 options. Some of the more popular ones include:
  , for example.

MMNumeric month (e.g.07)
MONAbbreviated month name (e.g.JUL)
MONTHFull month name (e.g.JULY)
DDDay of month (e.g.24)
DYAbbreviated name of day (e.g.FRI)
YYYY4-digit year (e.g.1998)
YYLast 2 digits of the year (e.g.98)
RRLike YY, but the two digits are ``rounded'' to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906
AM (or PM)Meridian indicator
HHHour of day (1-12)
HH24Hour of day (0-23)
MIMinute (0-59)
SSSecond (0-59)

No comments:

Post a Comment