Conversion functions convert a value from one datatype to another. Generally, the form of the function names follows the convention datatype
TO
datatype
. The first datatype is the input datatype. The second datatype is the output datatype. The SQL conversion functions are:
ROWIDTOCHAR
converts a rowid value to VARCHAR2
datatype. The result of this conversion is always 18 characters long.
The following example converts a rowid value in the employees
table to a character value. (Results vary for each build of the sample database.)
SELECT ROWID FROM employees
WHERE ROWIDTOCHAR(ROWID) LIKE '%SAAb%';
ROWID
------------------
AAAFfIAAFAAAABSAAb
TO_CHAR
(character) converts NCHAR
, NVARCHAR2
, CLOB
, or NCLOB
data to the database character set.
The following example interprets a simple string as character data:
SELECT TO_CHAR('01110') FROM DUAL;
TO_CH
-----
01110
The following example converts some CLOB
data from the pm.print_media
table to the database character set:
SELECT TO_CHAR(ad_sourcetext) FROM print_media
WHERE product_id = 2268;
TO_CHAR(AD_SOURCETEXT)
--------------------------------------------------------------------
******************************
TIGER2 2268...Standard Hayes Compatible Modem
Product ID: 2268
The #1 selling modem in the universe! Tiger2's modem includes call
management and Internet voicing. Make real-time full duplex phone
calls at the same time you're online.
**********************************
TO_CHAR
(datetime) converts date
of DATE
, TIMESTAMP
, TIMESTAMP
WITH
TIME
ZONE
, or TIMESTAMP
WITH
LOCAL
TIME
ZONE
datatype to a value of VARCHAR2
datatype in the format specified by the date format fmt
. If you omit fmt
, then date
is converted to a VARCHAR2
value as follows:
DATE
is converted to a value in the default date format. TIMESTAMP
and TIMESTAMP
WITH
LOCAL
TIME
ZONE
are converted to values in the default timestamp format. TIMESTAMP
WITH
TIME
ZONE
is converted to a value in the default timestamp with time zone format. The 'nlsparams
' specifies the language in which month and day names and abbreviations are returned. This argument can have this form:
'NLS_DATE_LANGUAGE = language'
If you omit 'nlsparams'
, then this function uses the default date language for your session.
SELECT to_char(sysdate,'yyyy/mm/dd HH24:MI:SS') FROM DUAL;
-------------
2010/10/12 14:28:17
默认秒的小数位为6
select TO_CHAR(SYSTIMESTAMP,'YYYY/MM/DD HH24:MI:SS.FF') from dual
-------------
2010/10/12 14:28:48.172236
指定秒的小数位为9
select TO_CHAR(SYSTIMESTAMP(9),'YYYY/MM/DD HH24:MI:SS.FF9') from dual
-------------
2010/10/12 14:29:20.272292000
TO_CHAR
(number) converts n
of NUMBER
datatype to a value of VARCHAR2
datatype, using the optional number format fmt
. If you omit fmt
, then n
is converted to a VARCHAR2
value exactly long enough to hold its significant digits.
The 'nlsparam'
specifies these characters that are returned by number format elements:
This argument can have this form:
'NLS_NUMERIC_CHARACTERS = ''dg''
NLS_CURRENCY = ''text''
NLS_ISO_CURRENCY = territory '
The characters d
and g
represent the decimal character and group separator, respectively. They must be different single-byte characters. Within the quoted string, you must use two single quotation marks around the parameter values. Ten characters are available for the currency symbol.
If you omit 'nlsparam'
or any one of the parameters, then this function uses the default parameter values for your session
The following statement uses implicit conversion to interpret a string and a number into a number:
SELECT TO_CHAR('01110' + 1) FROM dual;
TO_C
----
1111
select to_char(123+5) from dual
----
128
TO_CLOB
converts NCLOB
values in a LOB column or other character strings to CLOB
values. char
can be any of the datatypes CHAR
, VARCHAR2
, NCHAR
, NVARCHAR2
, CLOB
, or NCLOB
. Oracle executes this function by converting the underlying LOB data from the national character set to the database character set.
The following statement converts NCLOB
data from the sample pm.print_media
table to CLOB
and inserts it into a CLOB
column, replacing existing data in that column.
UPDATE PRINT_MEDIA
SET AD_FINALTEXT = TO_CLOB (AD_FLTEXTN);
TO_DATE
converts char
of CHAR
, VARCHAR2
, NCHAR
, or NVARCHAR2
datatype to a value of DATE
datatype.
select to_date('2010/01/01','yyyy/mm/dd') from dual
----
2010-1-1
TO_NUMBER
converts char
, a value of CHAR
, VARCHAR2
, NCHAR
, or NVARCHAR2
datatype containing a number in the format specified by the optional format model fmt
, to a value of NUMBER
datatype.
select TO_NUMBER('100.00') from dual
----
100
TO_YMINTERVAL
converts a character string of CHAR
, VARCHAR2
, NCHAR
, or NVARCHAR2
datatype to an INTERVAL
YEAR
TO
MONTH
type, where char
is the character string to be converted.
select to_date('2010/01/01','yyyy/mm/dd') + TO_YMINTERVAL('01-02') from dual
----
2011-3-1