1. 程式人生 > >Oracle中三個型別轉換函式的使用

Oracle中三個型別轉換函式的使用

Oracle中有三個型別轉換函式,分別是to_char()、to_date()以及to_number()函式

一、to_char()函式的用法

(1)轉化日期、時間

to_char(data,'格式')

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')時間  from dual;

時間
-------------------
2018-12-26 16:37:23

(2)處理數字型別

to_char(number,'格式')

select to_char(456789)結果 from dual;

結果
------
456789

select to_char(456789,'999999')結果 from dual;

結果
-------
 456789

(3)數字轉特殊型別

select to_char(456789,'$999999')結果 from dual;

結果
--------
 $456789

select to_char(456789,'L999999')結果 from dual;

結果
-----------------
         ¥456789

(4)用於進位制間的轉換

select to_char(12345678,'xxxxxxxx')十六進位制 from dual;

十六進位制
---------
   bc614e

除此之外,to_char()函式還有很多功能,這裡就不一一給出,可以自行搜尋to_char()的額外功能。

二、to_date()函式的用法

1)以24小時制處理時間

select to_date('2018-9-18 13:28:59','yyyy-MM-dd HH24:mi:ss')時間轉換 from dual;

時間轉換
--------------
18-9月 -18

(2)to_date()與to_char()函式的轉化

select to_char(sysdate,'yyyy-MM-dd')型別轉換 from dual;

型別轉換
----------
2018-12-26

select to_date('2018-12-26','yyyy-MM-dd')型別轉換 from dual;

型別轉換
--------------
26-12月-18

三、to_number()函式的用法

to_number()主要是將字串型別轉化為數值型型別,作用與to_char()函式剛好相反。

to_number('字串','格式')

to_number()系統定義的固定格式:

格式值 作用
0 強制0顯示(如null值可以使用0來強制顯示)
9 表示一個數字型別
L 顯示本地貨幣符號如:¥
$ 顯示美元符號
. 顯示小數點(99.99)
顯示千位分隔符號(999,999)

例如:

select to_number('456789')字元轉數字 from dual;

字元轉數字
----------
    456789
select to_number('456,789','999,999')字元轉數字 from dual;

字元轉數字
----------
    456789
select to_number('¥1234','L9999')字元轉數字 from dual;

字元轉數字
----------
      1234
select to_number('1234.23','9999.99')字元轉數字 from dual;

字元轉數字
----------
   1234.23