String and regular expression replace #1454
-
Overview
Syntax: NOTE: This is an SQL standard function for the case when pattern is a string.
Examples-- pattern is a string:
Replace('Hello World* World*', 'World*', 'WWorld**') => 'Hello WWorld** WWorld**'
Replace('Hello World World', 'world', 'everyone') => 'Hello World World' -- No matches (case sensitive)
Replace('Hello World World', 'world', 'everyone') ci => 'Hello everyone everyone'
Replace('Hello World World', 'W', '') => 'Hello orld orld'
Replace('Hello World World', '', 'everyone') => 'Hello World World' -- Pattern '' never matches (see Microsoft SQL Server)
Replace(null, '', 'everyone') => null
Replace('Hello World World', '', null) => null
Replace(null, '', null) => null
-- pattern is a regular expression:
Replace('Hello World', /l./, 'X') => 'HeXo WorX'
Replace('Hello World', /[HW]/, '') => 'ello orld'
Replace('Hello World', /(h.+) (w.+)/, 'First $1$1 Second $2$2') => 'Hello World' -- No matches (case sensitive)
Replace('Hello World', /(h.+) (w.+)/, 'First $1$1 Second $2$2') ci => 'HelloHello WorldWorld'
Replace(null, /l./, 'X') => null
Replace(null, /l./, null) => null
-- pattern is null:
Replace(null, null, null) => null
Replace('A', null, null) => null
Replace(null, null, 'B') => null Precise DefinitionVarious edge cases are resolved to be consistent with these .NET/C# functions. string Replace(string text, Regex pattern, string replacement)
{
if (text == null || pattern == null || replacement == null)
return null;
return pattern.Replace(text, replacement);
}
string Replace(string text, string pattern, string replacement)
{
if (text == null || pattern == null || replacement == null)
return null;
if (pattern.Length == 0)
return text; // Pattern '' never matches (see Microsoft SQL Server)
return text.Replace(pattern, replacement);
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thanks for this suggestion @jasonkres; we're not expecting it will make it into 2022.1 or .2, but we're keen to continue the thread so will move it to a discussion. |
Beta Was this translation helpful? Give feedback.
-
We'll include this in the next major version. I've moved this to a new ticket: #2236 - thanks @jasonkres! 😎 |
Beta Was this translation helpful? Give feedback.
We'll include this in the next major version. I've moved this to a new ticket: #2236 - thanks @jasonkres! 😎