oracle中CAST函數使用簡介【轉】

來源:互聯網
上載者:User

標籤:排名   sys   表示   desc   分數   結果   insert   需要   成績   

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

oracle中CAST函數使用簡介【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.