T-SQL Tutorial

Regular expressions


SQL Server regular expressions are a powerful tool for pattern matching and searching within text data stored in a database. A regular expression is a sequence of characters that define a search pattern, and it is used to search for and manipulate text based on that pattern.

SQL Server supports regular expressions through the use of the LIKE operator and the PATINDEX function. The LIKE operator is used to compare a column value to a pattern and return a boolean result, while the PATINDEX function returns the starting position of the first occurrence of a pattern within a string.

SQL Server also supports regular expressions through the use of the regex_replace function in the CLR Integration feature, which allows users to write and use custom functions in .NET languages like C# and Visual Basic.

Regular expressions in SQL Server can be used to perform a wide variety of text-related operations, including:

  • Finding patterns within text
  • Replacing or removing specific characters or strings within text
  • Splitting text into substrings based on a delimiter or pattern
  • Validating input data to ensure it conforms to a specific pattern or format


To use regular expressions in SQL Server, you need to be familiar with the syntax and rules that define the pattern matching logic. SQL Server uses a subset of the standard POSIX regular expression syntax, which includes the following operators and expressions:
  • Character classes: A set of characters defined within square brackets that match any one of the characters in the set.
  • Quantifiers: Used to specify the number of times a pattern should match, including * (zero or more), + (one or more), and ? (zero or one).
  • Alternation: The | character is used to define alternate patterns that can match a string.
  • Anchors: These are used to define the start and end of a string, including ^ (start of string) and $ (end of string).
  • Grouping: Parentheses () are used to group patterns together and apply quantifiers to the entire group.


Examples

SQL Server supports regular expressions using the LIKE operator with the % and _ wildcard characters, as well as the PATINDEX and REPLACE functions with regular expression syntax. Here are some examples of how to use regular expressions in SQL Server:

1. Matching a specific pattern using LIKE:

SELECT *
FROM myTable
WHERE myColumn LIKE 'ABC%';

This will return all rows where myColumn starts with the characters "ABC".

2. Matching any single character using _ wildcard character:

SELECT *
FROM myTable
WHERE myColumn LIKE 'A_C';

This will return all rows where myColumn starts with "A", followed by any single character, and ends with "C".

3. Matching any number of characters using % wildcard character:

SELECT *
FROM myTable
WHERE myColumn LIKE '%ABC%';

This will return all rows where myColumn contains the characters "ABC" anywhere within the column.

4. Using regular expression syntax with PATINDEX function:

SELECT *
FROM myTable
WHERE PATINDEX('%[0-9]%', myColumn) > 0;

This will return all rows where myColumn contains any digit from 0 to 9.

5. Using regular expression syntax with REPLACE function:

SELECT REPLACE(myColumn, '[aeiou]', '*')
FROM myTable;

This will replace all vowels (a, e, i, o, u) in myColumn with an asterisk (*) in all rows of myTable.

In addition to the basic regular expression syntax, SQL Server also supports a number of advanced features, such as backreferences, lookahead and lookbehind assertions, and quantifiers. These advanced features can be used to create even more complex search patterns.

Overall, SQL Server regular expressions provide a powerful tool for searching and manipulating text data within a database. By mastering regular expressions, you can make your SQL queries more flexible and powerful, and gain deeper insights into your data.