Home  >  Article  >  Database  >  How to convert data to numeric type in oracle

How to convert data to numeric type in oracle

青灯夜游
青灯夜游Original
2022-02-22 17:49:4935241browse

在oracle中,可以使用to_number()函数来将数据转为数字类型,该函数可以将字符串转换为数值型的格式,语法“to_number(varchar2 or char,'格式')”。

How to convert data to numeric type in oracle

本教程操作环境:Windows7系统、Oracle 11g版、Dell G3电脑。

在oracle中,可以使用to_number()函数来将数据转为数字类型。

to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式。

1、to_number()函数可以将char或varchar2类型的string转换为一个number类型的数值;

2、需要注意的是,被转换的字符串必须符合数值类型格式,如果被转换的字符串不符合数值型格式,Oracle将抛出错误提示;

3、to_number和to_char恰好是两个相反的函数; 

语法:

to_number(varchar2 or char,'格式')

To_number函数中也有很多预定义的固定格式:

格式值 含义
9 代表一个数字
0 强迫0显示
$ 显示美元符号
L 强制显示一个当地的货币符号
. 显示一个小数点
, 显示一个千位分隔符号

一些例子

select to_number('000012134') from dual;  
select to_number('88877') from dual;
SQL> select to_number(’RMB234234.4350′,’L999999.0000′) from dual;
    TO_NUMBER(’RMB234234.4350′,’L999999.0000′)
    ——————————————
    234234.435
SQL> select to_number(’$123,233,455,623.3400′,’$999,999,999,999.0000′) from dual;
    TO_NUMBER(’$123,233,455,623.3400′,’$999,999,999,999.0000′)
    ———————————————————-
    1.2323E+11

如果数字在格式范围内的话,就是正确的,否则就是错误的;如:

select to_number('$12345.678', '$999999.99') from dual;  
select to_number('$12345.678', '$999999.999') from dual;

to_number()函数可以用来实现进制转换;16进制转换为10进制:

select to_number('19f','xxx') from dual;  
select to_number('f','xx') from dual;

推荐教程:《Oracle教程

The above is the detailed content of How to convert data to numeric type in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is oracle functionNext article:what is oracle function