Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Type conversion of user data by Oracle cast function

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Cast function

The cast function is used to convert the data entered by the user. For example, when the data of user data is of number type, and the number of decimal places is 3 places, we can convert user data to int type of × × data.

The format of the cast function is cast (data source as data type to be converted)

For example, I need to convert 13.658 of the number data type to an integer type of int type

SQL > select cast (13.658 as int) from dual

CAST (13.658ASINT)

-

fourteen

From the figure above, we can see that cast rounds the data of the number data type and saves it as × × data.

The effect above is similar to the round function.

However, the result of the cast function does not affect the database table structure, only the user data is displayed. Examples are as follows:

-- the data records of 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 (20) Y

-- We use the cast function to convert the sno field to number (8jue 4)

SQL > select cast (sno as number (8pm 4)) from info

CAST (SNOASNUMBER (8, 4))

--

1.0000

2.0000

3.0000

-- but re-look up the info table and the result is as follows

SQL > select * from info

SNO SNAME

1 lilei

2 dushuai

3 caolirong

-- use the update function to update info tables

SQL > update info set sno=cast (sno as number (3)) where sno=1

1 row updated

SQL > commit

-- re-look up the info table, and the result is as follows

SQL > select * from info

SNO SNAME

1 lilei

2 dushuai

3 caolirong

If you want to use the cast function to convert data types, you can consider creating a view based on the base table or creating another table. The following example is to create a view based on the base table to change the data type:

SQL > create or replace view v_info as select cast (sno as number (3)) 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 (3pyr2) Y

SQL > desc info

Name Type Nullable Default Comments

--

SNO INTEGER

SNAME VARCHAR2 (20) Y

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report