Oracle中CAST函數使用簡介

Oracle中CAST函數使用簡介



CAST()函數能夠進行數據類型的轉換。ide

CAST()函數的參數有兩部分,源值和目標數據類型,中間用AS關鍵字分隔。函數

如下例子均經過本人測試。測試

1、轉換列或值ip

語法:cast( 列名/值 as 數據類型 )ci

用例:字符串

1)、轉換列it

--將empno的類型(number)轉換爲varchar2類型。io

select cast(empno as varchar2(10)) as empno from emp;table

EMPNO
----------
7369
7499
7521
...ast

2)、轉換值

--將字符串轉換爲整型。
SELECT CAST('123' AS int) as result from dual;

  RESULT
  ---

  123
返回值是整型值123。

--若是試圖將一個表明小數的字符串轉換爲整型值,又會出現什麼狀況呢?
SELECT CAST('123.4' AS int) as result from dual;

 RESULT
--------

  123

SELECT CAST('123.6' AS int) as result from dual;

 RESULT
--------

  124
從上面能夠看出,CAST()函數能執行四捨五入操做。

--截斷小數

SELECT CAST('123.447654' AS decimal(5,2)) as result from dual;

 RESULT
-----------
 123.45
decimal(5,2)表示值總位數爲5,精確到小數點後2位。
SELECT CAST('123.4' AS decimal) as result from dual;
結果是一個整數值:
123
2、轉換一個集合

語法:cast( multiset(查詢語句) as 數據類型 )

1)轉換成table

例子:

--學生成績表

create table stu_score
(stu_no varchar2(50),--學號
 score  number--總分
 );
insert into stu_score values('201301',67);
insert into stu_score values('201302',63);
insert into stu_score values('201303',77);
insert into stu_score values('201304',68);
insert into stu_score values('201305',97);
insert into stu_score values('201306',62);
insert into stu_score values('201307',87);
commit;

------------------------------------------

select * from stu_score;

學號         分數

--------   ----------
201301       67
201302       63
201303       77
201304       68
201305       97
201306       62
201307       87

--獎學金錶。

--獎學金錶規定了名次,每一個名次的人數和獎金。

create table scholarship
(
stu_rank   varchar(10),--名次
stu_num     int,--限定人數
money       number--獎金
);
insert into scholarship values('1',1,'1000');
insert into scholarship values('2',2,'500');
insert into scholarship values('3',3,'100');
commit;

-----------------------------------------------

select * from scholarship;

名次                                          人數     獎金
---------- --------------------------------------- ----------
1                                              1       1000
2                                              2        500
3                                              3        100

如今要根據成績表的成績降序排列,按獎學金錶的名額肯定排名和獎金。排名時不考慮相同成績。
排名的結果應該以下:
學號          成績        名次   獎金
201305        97          1        1000
201307        87           2        500
201303        77          2         500
201304        68          3         100
201301        67          3         100
201302        63          3         100

 

SELECT c.stu_no,c.score,b.stu_rank,b.money
  FROM (SELECT c.*,ROW_NUMBER() OVER(ORDER BY score DESC) rn FROM stu_score c) c
      ,(SELECT b.stu_rank,b.money,ROW_NUMBER() OVER(ORDER BY b.stu_rank) rn
         FROM scholarship b
            , TABLE( CAST( MULTISET( SELECT NULL
                                      FROM DUAL
                                   CONNECT BY LEVEL <= b.stu_num
                                   )
                            AS SYS.ODCIVARCHAR2LIST ) 
                           )
       ) b
WHERE c.rn=b.rn;

執行結果以下:

STU_NO                                                  SCORE      STU_RANK        MONEY
-------------------------------------------------- ----------         ----------          ----------
201305                                                     97                     1                1000
201307                                                     87                     2                 500
201303                                                     77                     2                 500
201304                                                     68                     3                 100
201301                                                     67                     3                 100
201302                                                     63                     3                 100

經過對比發現,確實達到了目的。

此外cast還能轉化成collection,varray,此時都須要記過multiset集合函數一塊兒使用。 

相關文章
相關標籤/搜索