Friday, January 21, 2011

Use the TO_CHAR, TO_NUMBER, and TO_DATE conversion functions

TO_CHAR Function - it  converts a date, number to a TEXT expression in a specified format. It means it returns an item of datatype VARCHAR2.
TO_CHAR function has following syntax:
TO_CHAR(number1, [format],[nls_parameter])
As we can see to_char has 1 mandatory parameter and 2 optional parameres.
Examples:
SQL> SELECT TO_CHAR(00001.1, 9) FROM DUAL;
TO
--
 1

SQL> SELECT TO_CHAR(00001.1, 9999.99) FROM DUAL;
TO_CHAR(
--------
    1.10

SQL> SELECT TO_CHAR(SYSDATE,'MONTH') FROM DUAL;
TO_CHAR(S
---------
JANUARY

TO_DATE - it will convert either a character string or an expression into a date value.
TO_DATE(string, [format],[nls_parameter]). The 'format' must be a valid DATE format: YYYY=year, MM=month, DD=Day, HH=Hour, Mi=Minute
If no format is specified Oracle will assume the default date format has been supplied in char. Check here
Examples:"
SQL> SELECT TO_DATE(
  2      'January 15, 1989, 11:00 A.M.',
  3      'Month dd, YYYY, HH:MI A.M.',
  4       'NLS_DATE_LANGUAGE = American')
  5       FROM DUAL;

TO_NUMBER - it returns an item of type NUMBER. The syntax for the to_number function is:
to_number( string1, [ format_mask ], [ nls_language ] )
The 'format' must be a valid Number format. If we use shorter format mask, an error will be returned. If we use longer format mask, the original number will be returned.

SQL> select to_number('123.65', '9999.99') from dual;
TO_NUMBER('123.65',9999.99)
---------------------------
                     123.65

No comments:

Post a Comment