項目不少業務表中存儲的信息都是以逗號分隔的業務數據,在統計、轉碼的時候非常不便,使用 with X('','') as values('','') 對結果方便遞歸操做。db2一個字段由多個數據以逗號分隔,分隔的數據爲字典表的code,如今要經過sql將該字段直接轉換爲字典code對應的name,參考下圖:sql
圖1轉成圖二spa
圖一code
圖二blog
WITH n ( str, ori, pos, USER ) AS ( VALUES ( concat('1,2',','), 1, posstr(concat('1,2' ,','), ','), '402882775f0e5084015f10cdaa00006a' ) UNION ALL SELECT str, pos+1, locate(',', str, pos+1), USER FROM n WHERE locate(',', str, pos+1)>0 ) SELECT LISTAGG(s.CODE_NAME,',') WITHIN GROUP( ORDER BY n.USER) FROM n, SYS_CODE_LIST s WHERE s.CODE_ID = substr(str,ori, CASE pos-ori WHEN -1 THEN 32 ELSE pos - ori END)