First, cast function
The cast function is a data type conversion of the data entered by the user, such as the number type of the data for the user data, the scale is 3 bits, and we can convert the user data into an int type of shaping data.
The format of the cast function is cast (data source as the data type to be converted)
For example, I need to convert 13.658 of the Number data type to integer type int
Sql> Select CAST (13.658 as int) from dual;
CAST (13.658ASINT)
---------------------------------------
14
By the We can see, cast to the number data type of data to be rounded and preserved into shaping data;
The above effect is similar to the round function.
But the result of the cast function does not work on the database table structure, only the user data is displayed. Examples are as follows:
The data records for the--info table are as follows:
Sql> SELECT * from info;
SNO SNAME
--------------------------------------- --------------------
1 Lilei
2 Dushuai
3 Caolirong
--info table field Structure
DESC Info
Name Type Nullable Default Comments
----- ------------ -------- ------- --------
SNO INTEGER
SNAME VARCHAR2 (Y)
--We use the CAST function to convert the data type of the Sno field to number (8,4)
Sql> Select CAST (sno as number (8,4)) from info;
CAST (Snoasnumber (8,4))
----------------------
1.0000
2.0000
3.0000
--but to find the info table again, the results are as follows
Sql> SELECT * from info;
SNO SNAME
--------------------------------------- --------------------
1 Lilei
2 Dushuai
3 Caolirong
--Update the Info table with the update function
Sql> Update info Set Sno=cast (sno as number (3,2)) where sno=1;
1 row updated
Sql> commit;
--Find the Info table again with the following results
Sql> SELECT * from info;
SNO SNAME
--------------------------------------- --------------------
1 Lilei
2 Dushuai
3 Caolirong
If you want to use the CAST function to transform a data type, consider creating a view based on a base table or a new table. The following example creates a view based on a base table and then implements the purpose of changing the data type:
Sql> Create or Replace view V_info as select CAST (Sno as number (3,2)) as V_sno from info;
View created
Sql> select * from V_info;
V_sno
-----
1.00
2.00
3.00
sql> desc V_info;
Name Type Nullable Default Comments
----- ----------- -------- ------- --------
V_sno number (3,2) Y
Sql> desc Info
Name Type Nullable Default Comments
----- ------------ -------- ------- --------
SNO INTEGER
SNAME VARCHAR2 (Y)
This article is from the "Oralce Learning path" blog, make sure to keep this source http://dushuai.blog.51cto.com/9461011/1568249
Oracle cast functions type conversion of user data