oracle 字串轉成行

2021-04-29 03:48:26 字數 1710 閱讀 8913

oracle 將以逗號隔開的字串'85,86,87' 轉成行

select   substr (t.rpt_id,

instr (t.rpt_id,',',1,c.lv)+ 1,

instr (t.rpt_id,',',1,c.lv + 1)- (instr (t.rpt_id,',',1,c.lv)+ 1))

as rpt_id

from   (select ',' || '85,86,87' || ',' rpt_id,

length ('85,86,87' || ',') - nvl (length (replace ('85,86', ',')), 0) cnt

from dual) t,

(select level lv

from dual

connect by level <= length('85,86,87' || ',') - nvl(length(replace('85,86,87', ',')), 0)) c

where   t.cnt >= c.lv

說明:cnt表示串裡面有多少字元。

當是字串是表中的字段時,取level<=最大個數

如:1).取最大個數

select max(length(rpt_id || ',') -

nvl(length(replace(rpt_id, ',')), 0)) into v_c

from dim_audit_table@sjmh_inter;

2).l轉成行

select t.t_name,

t.t_name_comm,

t.t_column,

t.t_column_comm,

t.comment_nl,

t.comment_nl_time,

t.seq_user_id,

substr(t.rpt_id,

instr(t.rpt_id, '','', 1, c.lv) + 1,

instr(t.rpt_id, '','', 1, c.lv + 1) -

(instr(t.rpt_id, '','', 1, c.lv) + 1)) as rpt_id

from (select a.t_name,

a.t_name_comm,

a.t_column,

a.t_column_comm,

a.comment_nl,

a.comment_nl_time,

a.seq_user_id,

'','' || a.rpt_id || '','' rpt_id,

length(a.rpt_id || '','') -

nvl(length(replace(a.rpt_id, '','')), 0) cnt

from dim_audit_table@sjmh_inter a

where a.comment_nl is not null) t,

(select level lv from dual connect by level <= '||v_c||') c

where c.lv <= t.cnt;

注:如果是上面**是遠端的**,(select level lv from dual connect by level <= '||v_c||') c,不要用dual表,可以改用all_objects或user_objects, 要不然能查詢,但是把查詢出來的sql插入到某個表時,只能插入一行

字串轉成整數

實現atoi這個函式,將乙個字串轉換為整數。如果沒有合法的整數,返回0。如果整數超出了32位整數的範圍,返回int max 2147483647 如果是正整數,或者int min 2147483648 如果是負整數。該題目較為複雜,需要分類討論多種情況。題意為將字串中出現的第乙個完整的整數返回,包括...

Oracle Blob轉成字串

oracle資料庫中的blob欄位轉成字串的函式 1 utl raw.cast to varchar2 blob var 只支援blob長度小於2000的字段 mysql select count from score news online a where a.onlineflag 1 and t...

整數轉成字串

1.使用itoa函式 char itoa int value char string,int radix 原型說明 value 欲轉換的資料。radix 轉換後的進製數,可以是2進製 8進製 10進製 16進製制等。exmaple include include int main void 2.不利...