1. 程式人生 > >oralce 將一個字串裡的資料按逗號分隔去重再拼接成一個新的字串

oralce 將一個字串裡的資料按逗號分隔去重再拼接成一個新的字串

sql結果集某列值顯示如下:

'00010430, 00228290, 07046884,07046884, 07383953'

 希望顯示的結果

'00010430, 00228290,07046884, 07383953'

新建一個function 實現此功能

create or replace
FUNCTION        "SF_SPLIT_ACCOUNT_ID_LIST" (
    account_id_list IN VARCHAR2
)RETURN VARCHAR2 AS
v_account_id_list VARCHAR2(5000);

BEGIN

WITH ACCTS AS (
SELECT DISTINCT str from (
SELECT REGEXP_SUBSTR(replace(account_id_list,', ',','), '[^,]+', 1, LEVEL, 'i') AS STR  
  FROM DUAL  
CONNECT BY LEVEL <=  
           LENGTH(replace(account_id_list,', ',',')) 
           - LENGTH(REGEXP_REPLACE(replace(account_id_list,', ',','), ',', ''))+1))
SELECT wm_concat(STR) INTO v_account_id_list FROM ACCTS;

RETURN v_account_id_list;

END SF_SPLIT_ACCOUNT_ID_LIST;