You are on page 1of 2

Conversion Functions:

--> The function converts one data type to another data type.
--> Character data are always valid for oracle.
--> But problem with number and date data. They need conversions. So invalid state
of number & date should be
converted into valid state and vic versa.

Types Of Conversion:

--> Implicit data type conversion.


--> Explicit data type conversion.

Implicit data type conversion:

--> These type of conversion work according to the conversion specified by oracle.

LIKE

--> Character to number conversion succeed only if the character strings represent
a valid no.
--> Character to date conversion succeed only if the character strings represent a
valid date.

Explicit data type conversion:

--> SQL provides three functions to convert a value from one data type to another.
--> The explicit conversion functions are:

--> to_char() : To character conversion function.


--> to_number() : To Number Conversion function.
--> to_date() : To Date Conversion function.

to_char() Function: (Number to Character Conversion)

--> Converts number data type to a value of varchar2 data type.

Syntax: to_char(number/col,'format','nls_params')

--> 1st argument contains a valid number.


--> 2nd argument that is format that can be use to convert into users required
format.
--> 3rd argument that nls_params, it specifies the characters returned by the
number format elements.

Different types of format/indicator:

1) Group separator indicator: (G / ,)

Ex:

SQL> select to_char(12000,'99G999') from dual;


SQL> select to_char(12000,'99,999') from dual;

Note: 9 stands for place holder, we need to provide same no of 9 (place holder)
which is equals to the length of actual number.

2) Decimal indicator: (D / .)
Ex:
SQL> select to_char(12000,'99999D99') from dual;
SQL> select to_char(12000,'99999.99') from dual;

3) Currency Indicator: (L) : L9999 or 9999L

Ex:
SQL> select to_char(12000,'L99999') from dual;

O/P: $12000

SQL> select to_char(12000,'99999L') from dual;

O/P: 12000$

SQL> select to_char(12000,'L99999','nls_currency = Rs.') from dual;

O/P: Rs.12000

Ex:

SQL> select to_char(12000,'L99G999D99','nls_currency = Rs.') from dual;

O/P: Rs.12,000.00

to_char() Function: (Date to Character Conversion)

--> It is used to convert a date value from original format to user format (Date
data type to Varchar2 data type).

Syntax: to_char(date/col, 'format','nls_params')

Diffrent Format/Indicators:

1) AD or A.D. / BC or B.C. Indicator:

Ex:
SQL> select to_char(sysdate,'AD') from dual;

O/P : AD

SQL> select to_char(sysdate,'BC') from dual;

O/P : AD

2) AM or A.M. / PM or P.M.:

Ex:

SQL> select to_char(sysdate,'AM') from dual;

O/P: PM

SQL> select to_char(sysdate,'P.M.') from dual;

O/P: P.M.

You might also like