How to solve SQL Server: Replace with wildcards?

1.4K    Asked by AnneBell in SQL Server , Asked on May 27, 2024

 Does Microsoft SQL Server natively support some sort of replace function using wild cards? I gather that Regular Expressions are not available natively. I note that there is a PATINDEX function that can be used to bolt together a solution is there one something that does it more simply? For example REPLACE(data,'[xy]','q') to replace x or y with q.

Answered by Clare Matthews

The REPLACE built-in function does not support patterns or wildcards; only LIKE and PATINDEX do.

Assuming that you really just want the simple single-character replacement as shown in the question, then you can call REPLACE twice, one nested in the other, as follows: SELECT REPLACE( REPLACE('A B x 3 y Z x 943 yy!', 'X, 'q'), 'y', 'q');

Returns: A B q 3 q Z q 943 Q! If you do need more complex pattern matching/replacement, then you will need to do that via Regular Expressions, which can only be done via SQLCLR. Several SQLCLR RegEx functions are available in the Free version of the SQL# SQLCLR library (which I wrote), one of them being RegEx_Replace[4k]() (the 4k version is for when you are certain that you will never need more than 4000 characters, hence you can get better performance from not using NVARCHAR(MAX) as an input parameter or return value).

The equivalent of the two nested REPLACE calls would be done as follows (and using the pattern syntax shown in the question):

      SELECT SQL#.RegEx_Replace4k(N'A B x 3 y Z x 943 yy!', N'[xy]', N'q', -1, 1, NULL);

Returns: A B q 3 q Z q 943 Q!

But, since we are talking about the possibility of a more complex pattern, one that cannot be done easily in T-SQL, we can use a quantifier on the pattern to have it replace any number of contiguous x or y characters with a single q:

      SELECT SQL#.RegEx_Replace4k(N'A B x 3 y Z xx 943 yyxxyxy!', N'[xy]+', N'q', -1, 1, NULL);

Returns: A B q 3 q Z q 943 q!

Please note that the input string was changed slightly from the previous two examples to add an extra x after the Z, and to add an extra xxyxy to the yy at the end. And, in both cases, the multi-character fragment was replaced with a single q.

Hope this helps!



Your Answer

Answer (1)

In SQL Server, replacing strings with wildcards can be achieved using the REPLACE function combined with wildcards in the LIKE clause. The REPLACE function is used to substitute a substring within a string, while the LIKE clause with wildcards can be used to find patterns within strings.

Here’s a step-by-step guide on how to solve this:

Step-by-Step Solution

1. Identify the Pattern: Determine the pattern you need to replace. Wildcards in SQL Server include:

  • % for any sequence of characters (including an empty sequence).
  • _ for a single character.

2. Using REPLACE Function: The REPLACE function replaces all occurrences of a specified string value with another string value. The syntax is:

REPLACE (string_expression, string_pattern, string_replacement)

Combining with LIKE: You can use the LIKE operator to find rows that match a specific pattern and then use REPLACE to modify the matched patterns.

Example

Let's say you have a table named Employees with a column FullName and you want to replace occurrences of 'John' with 'Jonathan', but only when 'John' appears as a standalone word, not as part of another word (e.g., 'Johnathan').



Here’s how you can do it:

Step 1: Find the Rows

Use the LIKE clause to find rows where 'John' appears as a standalone word:

SELECT FullName
FROM Employees
WHERE FullName LIKE '% John %'
   OR FullName LIKE 'John %'
   OR FullName LIKE '% John'
   OR FullName = 'John';

Step 2: Replace the Substring

Use the REPLACE function to substitute 'John' with 'Jonathan':UPDATE Employees
SET FullName = REPLACE(FullName, ' John ', ' Jonathan ')
WHERE FullName LIKE '% John %';
UPDATE Employees
SET FullName = REPLACE(FullName, 'John ', 'Jonathan ')
WHERE FullName LIKE 'John %';
UPDATE Employees
SET FullName = REPLACE(FullName, ' John', ' Jonathan')
WHERE FullName LIKE '% John';
UPDATE Employees
SET FullName = REPLACE(FullName, 'John', 'Jonathan')
WHERE FullName = 'John';

Putting It All Together

You can combine these steps into a single script for clarity:

-- Replace ' John ' with ' Jonathan ' for middle occurrences

UPDATE Employees

SET FullName = REPLACE(FullName, ' John ', ' Jonathan ')
WHERE FullName LIKE '% John %';
-- Replace 'John ' with 'Jonathan ' for occurrences at the beginning
UPDATE Employees
SET FullName = REPLACE(FullName, 'John ', 'Jonathan ')
WHERE FullName LIKE 'John %';
-- Replace ' John' with ' Jonathan' for occurrences at the end
UPDATE Employees
SET FullName = REPLACE(FullName, ' John', ' Jonathan')
WHERE FullName LIKE '% John';
-- Replace 'John' with 'Jonathan' for exact match
UPDATE Employees
SET FullName = REPLACE(FullName, 'John', 'Jonathan')
WHERE FullName = 'John';

Notes

This example assumes you are replacing the string 'John' with 'Jonathan' only when 'John' is a separate word. Adjust the conditions if 'John' can be part of another word that should be replaced.

Be cautious with the REPLACE function as it is case-sensitive. For case-insensitive replacements, you might need to use a different approach or ensure your data is in a consistent case before replacing.

Always backup your data before performing bulk updates.

By following these steps, you can effectively replace substrings within your SQL Server database using wildcards and the REPLACE function.














4 Months

Interviews

Parent Categories