ORACLE中非常好用的型別轉換函式CAST

2021-04-13 05:16:15 字數 4931 閱讀 9176

cast with collections

using multiset with a varray

cast(multiset() as )

create or replace type cust_address_t

oid '53a970b3f5024bec8efd4f84cad5e09e'

as object (

street_address varchar2(40),

postal_code    varchar2(10),

city           varchar2(30),

state_province varchar2(2),

country_id     varchar(2));

/create or replace type address_book_t as table of cust_address_t;

/create table cust_address (

custno         number(10),

street_address varchar2(40),

postal_code    varchar2(10),

city           varchar2(30),

state_province varchar2(2),

country_id     varchar2(2));

insert into cust_address

values (1,'123 main st.','98040','mercer island','wa','us');

insert into cust_address

values (2,'1 broadway','10202','new york','ny','us');

insert into cust_address

values (3,'2462 edgar crest','v6l 2c4','vancouver','bc','cn');

commit;

create table cust_short (

custno number(10),

name   varchar2(30));

insert into cust_short values (1,'morgan');

insert into cust_short values (2,'cline');

insert into cust_short values (3,'scott');

select s.custno, s.name,

cast(multiset(select ca.street_address,

ca.postal_code,

ca.city,

ca.state_province,

ca.country_id

from cust_address ca

where s.custno = ca.custno) as address_book_t)

from cust_short s;

using multiset with a pl/sql table

cast(multiset() as )

create or replace type project_table_t as

table of varchar2(25);

/create table projects (

person_id    number(10),

project_name varchar2(20));

create table pers_short (

person_id number(10),

last_name varchar2(25));

insert into projects values (1, 'teach');

insert into projects values (1, 'code');

insert into projects values (2, 'code');

insert into pers_short values (1, 'morgan');

insert into pers_short values (2, 'cline');

insert into pers_short values (3, 'scott');

commit;

select * from projects;

select * from pers_short;

select e.last_name,cast(multiset(

select p.project_name

from projects p

where p.person_id = e.person_id

order by p.project_name) as project_table_t)

from pers_short e;

using multiset with a multi-column collection

cast(multiset() as )

create or replace type uob_type as object (

object_name varchar2(128), object_type varchar2(18));

/create or replace type t_uob_type as table of uob_type;

/set serveroutput on

declare

x t_uob_type;

begin

selectcast(multiset(

select object_name, object_type

from user_objects

where rownum <10) as t_uob_type)

into x

from dual;

for i in 1 .. x.count

loop

dbms_output.put_line(x(i).object_name || ' - '

|| x(i).object_type);

end loop;

end;

/converting a varray type column into a nested table

cast(as )

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);

set describe depth all linenum on indent on

desc region_tab

select * from region_tab;

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)));

selectcast(s.district as distlist_t)

from region_tab s

where s.reg_id = 30;

cast with dates

date

cast(as )

selectcast('01-jan-2004' as date) cdate

from dual;

timestamp

cast(as )

selectcast(sysdate as timestamp with local time zone) dtwtz

from dual;

cast with numbers

number

cast(as )

select 1 +cast(3.14 * 0.152 as number(10,7)) floating

from dual;

cast with strings

varchar2

cast(as )

select object_name

from user_objects;

selectcast(object_name as varchar2(30)) obj_name

from user_objects;

related topics

types

varrays  

介紹幾款開發中非常好用的軟體

notepad notepad 是一款windows環境下免費開源的 編輯器,功能十分強大。winmerge 檔案比較工具,在版本之間不同的時候查詢 變化的部分,十分方便 blog backup 可以備份csdn部落格,但是匯出html網頁功能需要註冊,不過不匯出也沒事。它把 上你部落格的內容給備份...

STL中非常好用的二分查詢函式彙總

1 搜尋某個元素是否存在,使用binary search beg,end,val 或binary search beg,end,val,op 2 獲得被搜尋元素的位置,應使用lower bound upper bound 或equal range 引數與以上相同 3 檢查若干個值是否存在bool i...

非常好用的通用mapper

2.1匯入的jar table 用在類上 宣告資料庫的表名 id 用在屬性上 宣告當前屬性為主鍵 column name username 作用在屬性上 用來指定資料庫中的欄位名稱 注意 建議使用駝峰命名法 資料庫中以下劃線分割如 userage 實體類中要使用駝峰規則如 userage 主鍵策略 ...