For a couple of mutually exclusive replacements, nested replace statements are the simplest and fastest way. Just like @Gordon suggests.
But that does not scale well for more than a few replacements and there are pitfalls:
Substrings
It becomes ambiguous when strings can be substrings of each other. Consider these two expressions:
SELECT replace((replace('2015 (Monday)', 'day', 'snake'), 'Monday', 'dog')
, replace((replace('2015 (Monday)', 'Monday', 'dog'), 'day', 'snake');
The result depends on the sequence of replacements. You have to define priorities. Typically you would replace longer strings first.
Chains
Then there is also the possibility that one replacement might create a match for the next:
SELECT replace((replace('2015 (Sunday)', 'Sun', 'Mon'), 'Monday', 'dog')
, replace((replace('2015 (Sunday)', 'Monday', 'dog'), 'Sun', 'Mon');
Again, you have to define priorities.
Every replacement possibly influences the next. With more than a few replacements, this becomes murky and error prone quickly. Also very hard to maintain if replacements can change.
As I said, with just the days of the week, nested replace()
statements are fine. That's not actually "dynamic". If weekdays were just to illustrate the problem and you actually have to deal with more cases or truly dynamic strings, I would consider a different approach. Find completely dynamic solutions in this related answer: