当前位置:  数据库>oracle

Oracle 中的类型转换函数

    来源: 互联网  发布时间:2017-04-08

    本文导语: 带小数点的字符串(除小数点外其它的都是数字)转换成数值 TO_NUMBER  Converts a string to the NUMBER data type TO_NUMBER([, , ]) RETURN NUMBER CREATE TABLE test (testcol VARCHAR2(10));INSERT INTO test VALUES ('12345.67');SELECT TO_BINARY_DOUBLE(testcol) BIN_DOUBLE, TO_B...

带小数点的字符串(除小数点外其它的都是数字)转换成数值

TO_NUMBER 

Converts a string to the NUMBER data type TO_NUMBER([, , ]) RETURN NUMBER CREATE TABLE test (
testcol VARCHAR2(10));

INSERT INTO test VALUES ('12345.67');

SELECT TO_BINARY_DOUBLE(testcol) BIN_DOUBLE, TO_BINARY_FLOAT(testcol) BIN_FLOAT, TO_NUMBER(testcol) NMBR
FROM test;





Converts a HEX number to FLOAT TO_NUMBER(, ); SELECT TO_NUMBER('0A', 'XX')
FROM dual;
Converts a HEX number to DECIMAL TO_NUMBER(,
'') RETURN ;
SELECT TO_NUMBER(100000,'XXXXXXXX')
FROM dual;

 

1.语法:TO_NUMBER(string[,format[,nlsparams]])
目的:将CHAR或VARCHAR2类型的string转换为一个NUMBER类型的数值,如果指定了format,那么string应该遵循相应的数字格式。
2.范例
DECLARE
    v_Num   NUMBER;
BEGIN
    v_Num   :=   TO_NUMBER( '$12345.67 ', '$99999.99 ');
END;






 
Oracle UTL_RAW
General Information Source {ORACLE_HOME}/rdbms/admin/utlraw.sql First Available 7.3.4 Constants Name Data Type Value big_endian PLS_INTEGER 1 little_endian PLS_INTEGER 2 machine_endian PLS_INTEGER 3 Dependencies 179 objects

SELECT name FROM dba_dependencies
WHERE referenced_name = 'UTL_RAW'
UNION
SELECT referenced_name FROM dba_dependencies
WHERE name = 'UTL_RAW';





Exceptions Error # Name Description VALUE_ERROR ORA-6502 An arithmetic, conversion, truncation, or size-constraint error. Usually raised by trying to cram a 6 character string into a VARCHAR2(5). Required Object Privileges GRANT execute on UTL_RAW GRANT execute ON utl_raw TO UWCLASS;   BIT_AND Perform bitwise logical "and" of the values in raw r1 with raw r2 and return the "anded" result raw utl_raw.bit_and(r1 IN RAW, r2 IN RAW) RETURN RAW; SELECT utl_raw.bit_and('0102F3', 'F30201')
FROM dual;
  BIT_COMPLEMENT Perform bitwise logical "complement" of the values in raw and return the "complement'ed" result raw utl_raw.bit_complement(r IN RAW) RETURN RAW; SELECT utl_raw.bit_complement('0102F3')
FROM dual;
  BIT_OR Perform bitwise logical "or" of the values in raw r1 with raw r2 and return the "or'd" result raw utl_raw.bit_or(r1 IN RAW, r2 IN RAW) RETURN RAW; SELECT utl_raw.bit_or('0102F3', 'F30201')
FROM dual;
  BIT_XOR Perform bitwise logical "exclusive or" of the values in raw r1 with raw r2 and return the "xor'd" result raw utl_raw.bit_xor(r1 IN RAW, r2 IN RAW) RETURN RAW; SELECT utl_raw.bit_xor('0102F3', 'F30201')
FROM dual;
  CAST_FROM_BINARY_DOUBLE Return the RAW representation of a binary_double value utl_raw.cast_from_binary_double(n IN BINARY_DOUBLE,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_from_binary_double(123.45)
FROM dual;
  CAST_FROM_BINARY_FLOAT Return the RAW representation of a binary_float value utl_raw.cast_from_binary_float(n IN BINARY_FLOAT,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;
SELECT utl_raw.cast_from_binary_float(123.45)
FROM dual;
  CAST_FROM_BINARY_INTEGER Return the RAW representation of a binary_integer value utl_raw.cast_from_binary_integer(
n         IN BINARY_INTEGER,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;

SELECT utl_raw.cast_from_binary_integer(100)
FROM dual;
  CAST_FROM_NUMBER Returns the binary representation of a NUMBER in RAW utl_raw.cast_from_number(n IN NUMBER) RETURN RAW; SELECT utl_raw.cast_from_number(100)
FROM dual;
  CAST_TO_BINARY_DOUBLE Perform a casting of the binary representation of the raw into a binary_double. cast_to_binary_double(
r         IN RAW,
endianess IN PLS_INTEGER DEFAULT 1) RETURN BINARY_DOUBLE;

SELECT utl_raw.cast_to_binary_double('405EDCCCCCCCCCCD')
FROM dual;
  CAST_TO_BINARY_FLOAT Perform a casting of the binary representation of the raw into a binary_float utl_raw.cast_to_binary_float(
n         IN BINARY_FLOAT,
endianess IN PLS_INTEGER DEFAULT 1) RETURN RAW;

SELECT utl_raw.cast_to_binary_float('42F6E666')
FROM dual;
  CAST_TO_BINARY_INTEGER Perform a casting of the binary representation of the raw into a binary integer utl_raw.cast_to_binary_integer(
r         IN RAW,
endianess IN PLS_INTEGER DEFAULT 1) RETURN BINARY_INTEGER;

SELECT utl_raw.cast_to_binary_integer('00000064')
FROM dual;
  CAST_TO_NUMBER Perform a casting of the binary representation of the number (in RAW) into a NUMBER utl_raw.cast_to_number(r IN RAW) RETURN NUMBER; SELECT utl_raw.cast_to_number('C202')
FROM dual;
  CAST_TO_NVARCHAR2
Converts a RAW represented using n data bytes into NVARCHAR2 with n data bytes
utl_raw.cast_to_nvarchar2(r IN RAW) RETURN NVARCHAR2; set serveroutput on

BEGIN
  FOR i IN 100..200 LOOP
    dbms_output.put_line(
    utl_raw.cast_to_nvarchar2(TO_CHAR(i)));
  END LOOP;
END;
/







  CAST_TO_RAW Converts a VARCHAR2 represented using n data bytes into a RAW with n data bytes utl_raw.cast_to_raw(r IN RAW) RETURN RAW; SELECT utl_raw.cast_to_raw('ABC')
FROM dual;
  CAST_TO_VARCHAR2 To extract a substring from a BLOB using a PL/SQL program use dbms_lob.substr(). The problem is that it returns a string in hexadecimal characters. CAST_TO_VARCHAR2 turns the hexadecimal string into readable ascii format. utl_raw.cast_to_nvarchar2(r IN RAW) RETURN VARCHAR2; set serveroutput on

BEGIN
  FOR i IN 100..200 LOOP
    dbms_output.put_line(utl_raw.cast_to_varchar2(TO_CHAR(i)));
  END LOOP;
END;
/






  COMPARE Compares raw r1 against raw r2. Returns 0 if r1 and r2 are identical, otherwise, returns the position of the first byte from r1 that does not match r2 utl_raw.compare(r1 IN RAW, r2 IN RAW,
pad IN RAW DEFAULT NULL) RETURN NUMBER;
SELECT utl_raw.compare(utl_raw.cast_to_raw('ABC'),
utl_raw.cast_to_raw('ACC'))
FROM dual;

  CONCAT
Concatenate a set of 12 raws into a single raw (up to 32K)
utl_raw.concat(r1 IN RAW DEFAULT NULL,
r2  IN RAW DEFAULT NULL,
r3  IN RAW DEFAULT NULL,
r4  IN RAW DEFAULT NULL,
r5  IN RAW DEFAULT NULL,
r6  IN RAW DEFAULT NULL,
r7  IN RAW DEFAULT NULL,
r8  IN RAW DEFAULT NULL,
r9  IN RAW DEFAULT NULL,
r10 IN RAW DEFAULT NULL,
r11 IN RAW DEFAULT NULL,
r12 IN RAW DEFAULT NULL) RETURN RAW;










SELECT utl_raw.concat('A','41','B','42')
FROM dual;
  CONVERT
Convert raw from one character to a different character set and return the resulting raw
utl_raw.convert(
r            IN RAW,
to_charset   IN VARCHAR2,
from_charset IN VARCHAR2) RETURN RAW;


DECLARE
 fr_charset CONSTANT VARCHAR2(30) :=
 SUBSTR(SYS_CONTEXT('USERENV', 'LANGUAGE'),
 INSTR(SYS_CONTEXT('USERENV', 'LANGUAGE'),'.')+1);

 to_charset VARCHAR2(30) := 'TR8EBCDIC1026S';
 rawvar     RAW(100);
BEGIN
  dbms_output.put_line(fr_charset);
  dbms_output.put_line(to_charset);

  rawvar := utl_raw.convert(UTL_RAW.CAST_TO_RAW('Morgan'),
  'AMERICAN_AMERICA.'||to_charset,
  'AMERICAN_AMERICA.'||fr_charset);

  dbms_output.put_line(rawvar);
END;
/
















  COPIES Return n copies of r concatenated together utl_raw.copies(r IN RAW, n IN NUMBER) RETURN RAW; SELECT utl_raw.copies('A', 6)
FROM dual;
  LENGTH Return the length in bytes of a raw utl_raw.length(r IN RAW) RETURN NUMBER; SELECT utl_raw.length('ABC')
FROM dual;
  OVERLAY Overlay the specified portion of target raw with overlay raw, starting from byte position pos of target and proceeding for len bytes utl_raw.overlay(overlay_str IN RAW, target IN RAW,
pos IN BINARY_INTEGER DEFAULT 1,
len IN BINARY_INTEGER DEFAULT NULL,
pad IN RAW DEFAULT NULL) RETURN RAW;


SELECT utl_raw.overlay('1', 'AAABBBCCC', 4)
FROM dual;
  REVERSE Reverse a byte sequence in raw r from end to end utl_raw.reverse(r IN RAW) RETURN RAW; SELECT utl_raw.reverse('123')
FROM dual;
  SUBSTR
Return a substring portion of raw r beginning at pos for len bytes
utl_raw.substr(r IN RAW,
pos IN BINARY_INTEGER,
len IN BINARY_INTEGER DEFAULT NULL) RETURN RAW;

set serveroutput on

DECLARE
 tmp  VARCHAR2(250) := '';
 vraw RAW(200) := utl_raw.cast_to_raw('ABCDEABCDEABCDE');
BEGIN
  FOR i IN 1 .. 15 LOOP
    tmp := tmp|| '0x' || utl_raw.substr(vraw, i, 1) || ' ';
    dbms_output.put_line(tmp);
  END LOOP;
END;
/










  TRANSLATE
Translate the bytes in the input r raw according to the bytes in the translation raws, from_set and to_set
utl_raw.translate(r IN RAW, from_set IN RAW, to_set IN RAW)
RETURN RAW;
CREATE OR REPLACE FUNCTION trans_demo(pin IN VARCHAR2)
RETURN VARCHAR2 IS
 r_in  RAW(2000);
 r_out RAW(2000);
 r_ul  RAW(64);
 r_lu  RAW(64);
BEGIN
  r_in := utl_raw.cast_to_raw(pin);
  r_ul := utl_raw.cast_to_raw('ABCDEFabcdef');
  r_lu := utl_raw.cast_to_raw('abcdefABCDEF');

  r_out := utl_raw.translate(r_in , r_ul, r_lu);

  return(utl_raw.cast_to_varchar2(r_out));
END trans_demo;
/

SELECT trans_demo('FaDe') FROM dual;
SELECT trans_demo('FAde') FROM dual;

















  TRANSLITERATE
Transliterate the bytes in the input r raw according to the bytes in the transliteration raws, from_set and to_set
utl_raw.transliterate(r IN RAW, to_set IN RAW DEFAULT NULL,
from_set IN RAW DEFAULT NULL, pad IN RAW DEFAULT NULL)
RETURN RAW;

CREATE OR REPLACE FUNCTION tl_demo(pin IN VARCHAR2)
RETURN VARCHAR2 IS
 r_in  RAW(2000);
 r_out RAW(2000);
 r_up  RAW(64);
 r_lo  RAW(64);
 r_un  RAW(64) := utl_raw.cast_to_raw('_');
BEGIN
  r_in := utl_raw.cast_to_raw(pin);
  r_up := utl_raw.cast_to_raw('ABCDEF. ');
  r_lo := utl_raw.cast_to_raw('abcdef');

  r_out := utl_raw.transliterate(r_in , r_lo, r_up, r_un);

  return(utl_raw.cast_to_varchar2(r_out));
END tl_demo;
/

SELECT tl_demo('AB C.D') FROM dual;

















  XRANGE
Returns a raw containing all valid 1-byte encodings in succession beginning with the value start_byte and ending with the value end_byte.
utl_raw.xrange(start_byte IN RAW DEFAULT NULL,
end_byte IN RAW DEFAULT NULL) RETURN RAW;
SELECT utl_raw.xrange(utl_raw.cast_to_raw('A'),
utl_raw.cast_to_raw('Z'))
FROM dual;

SELECT utl_raw.xrange(utl_raw.cast_to_raw('0A'),
utl_raw.cast_to_raw('Z'))
FROM dual;






    
 
 

您可能感兴趣的文章:

  • Oracle 系统变量函数介绍
  • Oracle 系统变量函数用法指南
  • Oracle中decode函数的用法
  • Oracle round()函数与trunc()函数区别介绍
  • oracle中lpad函数的用法详解
  • MySQL实现类似Oracle中的decode()函数的功能
  • Oracle函数substr(str1, pos, [len])
  • Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
  • SQL中Charindex和Oracle中对应的函数Instr对比
  • apache通过php的oci函数读取Oracle(字符集ZHS16GBK)时,显示乱码,如何解决?
  • Oracle Max函数使用中出现的问题
  • oracle合并列的函数wm_concat的使用详解
  • 请问:在使用oracle数据库作开发时,是使用pro*c作开发好些,还是使用库函数如oci等好一些啊?或者它们有什么区别或者优缺点啊?
  • c#中oracle to_date函数用法举例
  • Web服务器/前端 iis7站长之家
  • Oracle过程与函数的区别分析
  • Oracle层次查询和with函数的使用示例
  • oracle的nvl函数的使用介绍
  • c#中oracle的to_date函数使用方法
  • Oracle中nul()函数
  • 在oracle里如何将String转换成Date?????
  • 请问:ORACLE中的数据取出来后,需不需要进行一定的转换才能变为C语言的数据类型啊?
  • ORACLE 毫秒与日期的相互转换示例
  • 用java怎样实现oracle数据库表和excel数据表的转换
  • Oracle与FoxPro两数据库的数据转换步骤
  • oracle SCN跟TIMESTAMP之间转换
  • Oracle下时间转换在几种语言中的实现
  • 关于tomcat4.0.1+JDK1.3+ORACLE+JDBC中字符编码的转换问题。
  • MySQL转换Oracle的需要注意的七个事项
  • MySQL数据库向Oracle转换时注意若干问题
  • Oracle Number型数值存储与转换的实现详解
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • ORACLE数据库常用字段数据类型介绍
  • oracle中的空类型与c语言的空类型相匹配吗?
  • 关于Oracle BLOB类型,一个String字符怎么写入BLOB字段?
  • Oracle中查看某列数据类型
  • 测试添加Oracle中Blob数据类型对象
  • jsp文件上传smartupload到oracle数据库中没有longblob的数据类型如何处理的?
  • oracle中 VARCHAR2是什么数据类型
  • 请问保存文章内容的字段应用什么类型的(oracle),急!!!
  • oracle中怎么没有boolean类型的字段?应该用什么代替?
  • 如何向oracle库中字段类型是date的添加数据
  • jsp中在oracle中查询日期类型时sql语句该怎么写啊?
  • 怎样将当前时间写到Oracle中date类型的字段中!!!!!!!!!!!!
  • 急!急!oracle 中 long 类型在 tomcat 中的问题
  • 不能把几百个以上的汉字插入oracle varchar2类型的问题!
  • Oracle里long类型
  • jsp显示oracle中varchar2类型字段 在线等待
  • Oracle中的Raw类型解释
  • 我要向oracle中插入大文本,用的是lang类型的字段,但是只能插3000字,再多就抱错,说我字符串过长。谁遇到过此问题?
  • Oracle中字符集的类型决定varchar2的字符长度
  • 在Oracle的函数中,返回表类型的语句
  • 高分求救:谁用过JSP处理ORACLE数据库中的LONG RAW类型字段
  • Oracle 12c发布简单介绍及官方下载地址
  • 在linux下安装oracle,如何设置让oracle自动启动!也就是让oracle那个服务自动启动,不是手动的
  • oracle 11g最新版官方下载地址
  • 请问su oracle 和su - oracle有什么不同?
  • Oracle 数据库(oracle Database)Select 多表关联查询方式
  • 虚拟机装Oracle R12与Oracle10g
  • Oracle数据库(Oracle Database)体系结构及基本组成介绍
  • Oracle 数据库开发工具 Oracle SQL Developer
  • 如何设置让Oracle SQL Developer显示的时间包含时分秒
  • Oracle EBS R12 支持 Oracle Database 11g


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3