1.第一种方法 :oracle 创建函数
CREATE OR REPLACE FUNCTION sumstrsplitBySep(str VARCHAR2 ,sep VARCHAR2)
RETURN VARCHAR2
IS
RES VARCHAR2(100);
TEMP VARCHAR2(100) :=STR;
S_INDEX NUMBER;
BEGIN
LOOP
S_INDEX := NVL(INSTR(TEMP,SEP),0);
IF
S_INDEX=0 OR LENGTH(TEMP)=0 THEN
RES:=NVL(RES,0)+NVL(TEMP,0);
exit;
ELSE
res:=to_number(nvl(res,0))+to_number(substr(temp,0,s_index-1));
temp := substr(temp,s_index+1,length(temp));
END IF;
END LOOP;
RETURN RES;
EXCEPTION
WHEN others THEN return 0;
END sumstrsplitBySep;
Return = 6
2.第二种方法:使用正则表达式
WITH TEST AS (SELECT '1|2|3' AS STR FROM DUAL)
SELECT REGEXP_SUBSTR(STR,'[^|]+',1,LEVEL) AS SUMUSE FROM TEST
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(STR,'[^|]',''))+1
SELECT SUM(SUMUSE) FROM (
WITH TEST AS (SELECT '1|2|3' AS STR FROM DUAL)
SELECT REGEXP_SUBSTR(STR,'[^|]+',1,LEVEL) AS SUMUSE FROM TEST
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(STR,'[^|]',''))+1)