Conversion Functions

Conversion Functions

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

Purpose

ROWIDTOCHAR converts a rowid value to VARCHAR2 datatype. The result of this conversion is always 18 characters long.

Examples

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)

Purpose

TO_CHAR (character) converts NCHAR, NVARCHAR2, CLOB, or NCLOB data to the database character set.

Examples

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)

Purpose

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.

 

Examples

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)

Purpose

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:

  • Decimal character
  • Group separator
  • Local currency symbol
  • International currency symbol

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

Examples

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

Purpose

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.

Examples

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

Purpose

TO_DATE converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of DATE datatype.

Examples

select to_date('2010/01/01','yyyy/mm/dd') from dual

----

2010-1-1

 

TO_NUMBER

Purpose

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.

Examples

select TO_NUMBER('100.00') from dual

----

100

TO_YMINTERVAL

Purpose

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.

Examples

select to_date('2010/01/01','yyyy/mm/dd') + TO_YMINTERVAL('01-02') from dual

----

2011-3-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值