Back to Code Snippets


Multiple String ReplacementSQL

A macro to easily run multiple string replacements iteratively without function nesting.

Execute this SQL

CREATE MACRO replace_many (string, replacements) AS 
  list_reduce(
    replacements, 
    lambda acc, replacement: [  -- We wrap this in a list for type safety
        replace(acc[1], replacement[1], replacement[2])
    ], 
    [string]
  )[1];

-- Usage example
SELECT replace_many('Foo Bar', [['Foo', 'foofoo'], ['Bar', 'BarBar']]);

Copy code

Teague Sterling

Expand

Share link