The CAST () function for data type conversion.
The CAST () function parameters has two parts, values of the source and target data types, separated by the AS keyword.
The following examples are tested by himself.
A, or the column value conversion
Syntax: cast (column name / value as data type)
example:
1), Conversion Columns
- The empno type (number) is converted to varchar2 type.
select cast(empno as varchar2(10)) as empno from emp;
EMPNO
----------
7369
7499
7521
...
2), the conversion value
- convert a string to an integer.
SELECT CAST ( '123' AS int ) as result from dual;
RESULT
---
123
The return value is an integer value of 123.
- If you try to convert a string that represents the decimal integer value, what will happen then?
SELECT CAST ( '123.4' AS int ) as result from dual;
RESULT
--------
123
SELECT CAST('123.6' AS int) as result from dual;
RESULT
--------
124
can be seen from the above, CAST () function can perform rounding operations.
- truncate decimal
SELECT CAST('123.447654' AS decimal(5,2)) as result from dual;
The RESULT
-----------
123.45
decimal (5,2) represents the total number of bits of the value 5, 2 decimal place.
SELECT CAST ( '123.4' AS decimal ) as result from dual;
the result is an integer value:
123
Second, a set of conversion
Syntax: cast (multiset (query) as data type)
1) into a table
example:
- student achievement 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;
Student ID Score
-------- ----------
201301 67
201302 63
201303 77
201304 68
201305 97
201306 62
201307 87
- Scholarship table.
- Scholarship table specifies the ranking, each ranking number and bonuses.
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;
The number of bonus ranking
---------- ------------------------------------- - ----------
. 1. 1 1000
2 500 2
. 3. 3 100
Now to the results in descending order according to the results table to determine the ranking and bonuses according to the scholarship quota table. Does not consider the same results when ranking.
The results should be ranked as follows:
learning score ranking bonus number
201305. 1 1000 97
201307 87 500 2
201303 77 500 2
201304 68. 3 100
201301 67 100. 3
201302 63 is 100. 3
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;
Execution results are as follows:
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
By comparison, really it served its purpose.
Further cast can be converted into collection, varray, this time with the need to use a set of functions multiset demerit.
Reprinted: https: //www.cnblogs.com/hsz1124/p/7409994.html