1. 程式人生 > >Oracle 11G函式整理(轉換函式)

Oracle 11G函式整理(轉換函式)

1、ASCIISTR(ch) 轉換ch為ASCII字串

SELECT ASCIISTR('AB?CDE張三') a1 FROM DUAL;

2、BIN_TO_NUM(ch)  轉換位向量為一個數字[ch為逗號隔開的0或1]

SELECT BIN_TO_NUM(1,0,0,0) a1 FROM DUAL;--23次方[1000]

3、CAST(expr astype_name) 轉換expr為type_name型別[前提是能夠轉換才行]

SELECT CAST(to_date('2013-01-0101:01:01','YYYY-MM-DDHH24:MI:SS') AStimestampWITHLOCALTIME

ZONE) a1 FROM DUAL;

select CAST('12435'asnumber) a2 from dual;

4、CHARTOROWID(ch) 將字串轉換為ROWID型別

select CHARTOROWID('AAAFd1AAFAAAABSAA/') a1 from dual;

5、COMPOSE(ch)

6、CONVERT(ch,dest_ch_set,source_char_set)轉換字串為另一種字串

SELECT CONVERT('? ¨º ¨ª ? ? A B C DE ', 'US7ASCII', 'WE8ISO8859P1') a1 FROM DUAL;

7、DECOMPOSE(ch)

8、HEXTORAW(ch) 轉換十六進位制字串為RAW型別

SELECT HEXTORAW('4041424344') a1 FROM DUAL;

SELECT UTL_RAW.CAST_TO_VARCHAR2(HEXTORAW('4041424344')) a1 FROM DUAL;

9、NUMTODSINTERVAL(n,interval_unit) 將數字與單位轉換為內部時間格式, interval_unit格式有:DAY,HOUR,MINUTE,SECOND 【DS表示 DAY——>SECOND】

select sysdate+NUMTODSINTERVAL(10,'DAY')a1,sysdate+NUMTODSINTERVAL(10,'HOUR') a2,sysdate+NUMTODSINTERVAL(10,'MINUTE')a3,sysdate+NUMTODSINTERVAL(10,'SECOND') a4 from dual

10、NUMTOYMINTERVAL(n,interval_unit) 將數字與單位轉換為內部時間格式, interval_unit格式有:YEAR ,MONTH【YM表示 YEAR——>MONTH】

select sysdate+NUMTOYMINTERVAL(10,'YEAR') a1,sysdate+NUMTOYMINTERVAL(10,'MONTH')a2 from dual

11、RAWTOHEX(raw) 轉換RAW為十六進位制字串

12、RAWTONHEX(raw)= TO_NCHAR(RAWTOHEX(raw))

select  rowid,ROWIDTOCHAR(rowid) a1 from scott.emp;

14、ROWIDTONCHAR(rowid)=TO_NCHAR(ROWIDTOCHAR(rowid))

15、SCN_TO_TIMESTAMP(scn)將SCN轉換為時間戳

SELECT  SCN_TO_TIMESTAMP(ORA_ROWSCN) FROM tb_name WHERE pk_ID = 136512;

16、TIMESTAMP_TO_SCN(timestamp)將時間戳轉換為SCN

select TIMESTAMP_TO_SCN(systimestamp) a1 from dual

17、TO_BINARY_DOUBLE(expr,fmt,nlspara)返回雙精度型別資料

select TO_BINARY_DOUBLE(12) a1 from dual;

18、TO_BINARY_FLOAT(expr,fmt,nlspara)返回單精度型別資料

select TO_BINARY_FLOAT(12) a1 from dual;

19、TO_BLOB(raw_val) 轉換LONG RAW和RAW資料為BLOB型別

SELECT TO_BLOB(raw_col) blobFROM tb_name;

20、TO_CHAR (character) 轉換NCHAR,NVARCHAR2,CLOB,NCLOB為VARCHAR2型別

select to_char('Hello張三') a1 from dual;

21、TO_CHAR (datetime,fmt,nlspara)將時間相關;誒行轉換為VARCHAR2型別

select TO_CHAR(systimestamp, 'YYYY-MM-DDHH24:MI:SSxFF') a1 FROM dual;

SELECT TO_CHAR(INTERVAL'123-2'YEAR(3) TOMONTH) a1 FROM DUAL;

22、TO_CHAR(number,fmt,nlspara) 將數字相關型別NUMBER, BINARY_FLOAT, or BINARY_

DOUBLE轉換為VARCHAR2型別

SELECT TO_CHAR(-10000,'L99G999D99MI') a1 FROM DUAL;

SELECT TO_CHAR(-10000,'L99G999D99MI','NLS_NUMERIC_CHARACTERS= '',.''NLS_CURRENCY = ''AusDollars'' ') a1 FROM DUAL;

UPDATE tb_name SET clob_col = TO_CLOB(data);

24、TO_DATE(ch,fmt,nlspara)將字串轉換為Date型別

SELECT TO_DATE('January 15, 1989,11:00 A.M.','Month dd, YYYY,HH:MI A.M.','NLS_DATE_LANGUAGE =American') a1 FROM DUAL;

SELECT TO_DATE('2012-12-0110:01:02','YYYY-MM-DDHH24:MI:SS') a1 FROM DUAL;

25、TO_DSINTERVAL(fmt) 轉換一個格式字串為一個內部的 DAY——>SECOND

select  sysdate+TO_DSINTERVAL('10 00:00:00') a1 from dual;--10

select  sysdate+TO_DSINTERVAL(to_char(sysdate,'DD hh24:mi:ss')) a1 from dual;--加上當前時間的時分秒

26、TO_YMINTERVAL(fmt) 轉換一個格式字串為一個內部的 YEAR——>MONTH

select  sysdate+TO_YMINTERVAL('01-03') a1 from dual;--15個月

select  sysdate+TO_YMINTERVAL(to_char(sysdate,'yyyy-MM')) a1 from dual;

27、TO_LOB(long) 轉換long和long raw型別為LOB型別

SELECT TO_LOB(long_col) lob  FROM tb_name;

28、TO_MULTI_BYTE(ch) 將字串轉換為雙位元組表示

select'Hello張三') a1 from dual;

29、TO_NCHAR (character)轉換NCHAR,NVARCHAR2,CLOB,NCLOB為NVARCHAR2型別

select to_nchar('Hello張三') a1 from dual;

30、TO_NCHAR (datetime,fmt,nlspara)將時間相關;誒行轉換為NVARCHAR2型別

select  TO_NCHAR(systimestamp, 'YYYY-MM-DDHH24:MI:SSxFF') a1 FROM dual;

SELECT  TO_NCHAR(INTERVAL'123-2'YEAR(3) TOMONTH) a1 FROM DUAL;

31、TO_NCHAR (number,fmt,nlspara)將數字相關型別NUMBER, BINARY_FLOAT, or BINARY_

DOUBLE轉換為NVARCHAR2型別

SELECT TO_NCHAR(-10000,'L99G999D99MI') a1 FROM DUAL;

SELECT TO_NCHAR(-10000,'L99G999D99MI','NLS_NUMERIC_CHARACTERS= '',.''NLS_CURRENCY = ''AusDollars'' ') a1 FROM DUAL;

32、TO_NCLOB(ch) 將字串轉換為NCLOB型別

UPDATE tb_name SET nclob_col = TO_NCLOB(data);

33、TO_NUMBER(expr,fmt,nlspara)格式化數字輸出

SELECT TO_NUMBER('-AusDollars100','L9G999D99','NLS_NUMERIC_CHARACTERS = '',.''NLS_CURRENCY = ''AusDollars''') a1 FROM DUAL;

34、TO_SINGLE_BYTE(ch) 轉換為單位元組字元 ch型別可以是 CHAR,VARCHAR2, NCHAR, or NVARCHAR2,不能是CLOB

selectTO_SINGLE_BYTE('Hello張三') a1 from dual;

35、TO_TIMESTAMP(ch,fmt,nlspara)轉換為時間戳

SELECT  TO_TIMESTAMP('2013-09-3014:10:10.12320', 'YYYY-MM-ddHH24:MI:SS.FF') a1FROM DUAL;

36、TO_TIMESTAMP_TZ(ch,fmt,nlspara)轉換為帶時區的時間戳

SELECT  TO_TIMESTAMP_TZ('2013-09-3014:10:10.12320', 'YYYY-MM-dd HH24:MI:SS.FF') a1 FROM DUAL;

37、TRANSLATE (ch USINGCHAR_CS|NCHAR_CS) 將字串轉換為規定的字符集

select  TRANSLATE('Hello張三' USING CHAR_CS) a1,TRANSLATE('Hello張三' USING NCHAR_CS) a2 from dual

38、UNISTR(string) 將字串轉換為AL16UTF16或 UTF8字元

select  UNISTR('Hello張三') a1 from dual;