Exasol multi_replace Function

Exasol multi_replace Function

#exasol

Im Grunde auf Stackoverflow oder so geklaut und über Chatgpt übersetzt.. nach einigen Diskussionen mit ChatGpt kam dann doch was brauchbares raus:

CREATE OR REPLACE FUNCTION "MULTI_REPLACE" (pString VARCHAR(2000000) ,pReplacePattern VARCHAR(2000000))
RETURNS VARCHAR(2000000)
AS
iCount INT;
vResult VARCHAR(2000000);
vOldStr VARCHAR(2000000);
vNewStr VARCHAR(2000000);

BEGIN
iCount := 1;
vResult := pString;

-- Check that there are replacement rules specified
IF LENGTH(pReplacePattern) > 0 THEN 
    -- Use a loop to go through each replacement rule
    WHILE iCount <= (LENGTH(pReplacePattern) - LENGTH(REPLACE(pReplacePattern, ',',''))+1) do
        -- Use the REGEXP_SUBSTR function to extract the old and new string from the rule
        vOldStr := REGEXP_SUBSTR(pReplacePattern, '[^,]+', 1, iCount);
        vNewStr := REGEXP_SUBSTR(pReplacePattern, '[^,]+', 1, iCount+1);
        -- Do the replacement
        vResult := REPLACE(vResult, vOldStr, vNewStr);
        iCount := iCount + 2;
    END WHILE;
END IF;
RETURN vResult;
END;
/

Wie verwendet man es?

SELECT multi_replace('Hello World!', 'Hello,Hi,World,Universe');
Output:
Hi Universe!

ja billig es nicht selbst zu coden.. shame on me