cast函数
来源:互联网 发布:ubuntu root密码设置 编辑:程序博客网 时间:2024/06/05 21:33
cast函数
转自:http://blog.csdn.net/nvhaixx/article/details/6731936
作用:进行数据类型转换。
注意:可以直接将几个列直接转换成table,varray,Collection,NestedTable。此时需结合multiset集合函数一起使用。
1、转换一个列或值
语法:cast( 列名/值 as 数据类型 )
参数解释:1)列名/值 表中的列名
2)数据类型 转换后的数据类型
范例:select cast('2323' as char(6)) from dual;
select sysdate,cast(sysdate as timestamp with local time zone )from dual;
select cast(to_date('20110419010101','yyyy-mm-dd hh24:mi:ss') asdate) from dual;
转换成Nested Table
create or replace type district_t as OBJECT
(
region_no number(2),
title varchar2(35),
cost number(7, 2)
);
create type DistList_t as TABLE OF district_t;
create type DistrictList as VARRAY(10) OF district_t;
create table region_tab(reg_id number(2), reg_name varchar2(15),district DistrictList);
insert into region_tab values(30, 'Northwest',DistrictList(District_t(1, 'Alaska', 3250),District_t(2, 'Washington', 12350), District_t(3,'Oregon', 2750), District_t(4, 'Idaho', 1425)));
insert into region_tab values(40, 'Southwest',DistrictList(District_t(1, 'Arizona', 3250), District_t(2,'California', 12350), District_t(3, 'Nevada', 2750), District_t(4,'New Mexico', 1425)));
commit;
set describe depth all linenum on indent on;
desc region_tab;
select* from region_tab;
select cast(s.district as DistList_t)
from region_tab s
where s.reg_id = 30;
注意:不能指定显示的数据格式,且如果原本的数据值不满足转换的值的条件时,会报错。
范例:select cast(to_date('20110419','yyyy-mm-dd ') as date) fromdual;
2、转换一个集合
语法:cast( multiset(查询语句) as 数据类型 )
参数解释:1)查询语句 查询语句
2)数据类型 转换后的数据类型
3)multiset 查询返回的是多行
范例:1)转换成table
create or replace type project_table_t as tableof varchar2(25);
create table projects (person_idnumber(10),project_name varchar2(20));
insert into projects values(1, 'Teach');
insert into projects values(1, 'Code');
insert into projects values(2, 'Code');
commit;
create table pers_short (person_idnumber(10),last_name varchar2(25));
insert into pers_short values(1, 'Morgan');
insert into pers_short values(2, 'Cline');
转自:http://blog.csdn.net/nvhaixx/article/details/6731936
作用:进行数据类型转换。
注意:可以直接将几个列直接转换成table,varray,Collection,NestedTable。此时需结合multiset集合函数一起使用。
1、转换一个列或值
语法:cast( 列名/值 as 数据类型 )
参数解释:1)列名/值
范例:select cast('2323' as char(6)) from dual;
注意:不能指定显示的数据格式,且如果原本的数据值不满足转换的值的条件时,会报错。
范例:select cast(to_date('20110419','yyyy-mm-dd ') as date) fromdual;
2、转换一个集合
语法:cast( multiset(查询语句) as 数据类型 )
参数解释:1)查询语句
范例:1)转换成table