oracle 字串轉成行

來源:互聯網
上載者:User

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插入到某個表時,只能插入一行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.