oracle中CAST函數使用簡介

原文鏈接:https://www.cnblogs.com/hsz1124/p/7409994.html

 

CAST()函數可以進行數據類型的轉換。

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

以下例子均通過本人測試。

一、轉換列或值

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

用例:

1)、轉換列

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

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

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

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
二、轉換一個集合

語法: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集合函數一起使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章