Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
The following regular expressions can replace characters or digits in the Find what field of the SQL Server Management Studio Find and Replace dialog box.
Prerequisites
How to enable regular expressions
Here are steps to enable regular expressions in search.
- Go to Edit > Find and Replace > Quick Find.
- Next to the search bar select the down arrow > Find in Files.
- In the Find and Replace window, expand Find options, and select Use Regular Expressions.
The Expression Builder button next to the Find what field then becomes available. Select this button to display a list of the available regular expressions. When you choose any item from the Expression Builder, it is inserted into the Find what string.
The following table describes some of the regular expressions in the Expression Builder.
Expression | Description |
---|---|
. |
Match any single character (except a line break) |
.* |
Match any character zero or more times |
.+ |
Match any character one or more times |
[abc] |
Match any character in the set abc |
[^abc] |
Match any character not in the set abc |
\d |
Match any numeric character |
(?([^\r\n])\s) |
Match any whitespace character |
\b |
Match at the beginning or end of the word |
^ |
Match at beginning of line |
.$ |
Match any line break |
\w\r?\n |
Match a word character at end of line |
(dog | cat) |
Capture and implicitly number the expression dog | cat |
(?<pet>dog | cat) |
Capture subexpression dog | cat and name it pet |
Examples
Some examples of using regular expressions.
Example 1: Find all select statements
You want to find all SELECT
statements in your T-SQL scripts.
SELECT\s+.*\s+FROM
Example 1 explanation
SELECT\s+
: Matches the wordSELECT
followed by one or more whitespace characters..*
: Matches any character (except for line terminators) zero or more times.\s+FROM
: Matches one or more whitespace characters followed by the wordFROM
.
Example 2: Find procedures with specific naming patterns
You want to find all stored procedures that start with usp_
in your T-SQL scripts.
CREATE\s+PROCEDURE\s+usp_[A-Za-z0-9_]+
Example 2 explanation
CREATE\s+PROCEDURE\s+
: Matches the wordsCREATE PROCEDURE
followed by one or more whitespace characters.usp_
: Matches the literal stringusp_
.- [A-Za-z0-9_]+: Matches one or more alphanumeric characters or underscores.
Example 3: Find comments in T-SQL scripts
You want to identify all single-line comments (starting with --
) in your T-SQL scripts.
--.*
Example 3 explanation
--
: Matches the literal string--
..*
: Matches any character (except for line terminators) zero or more times.
Example 4: Find all update statements
You want to find all the UPDATE
statements in your T-SQL scripts.
UPDATE\s+.*\s+SET
Example 4 explanation
UPDATE\s+
: Matches the wordUPDATE
followed by one or more whitespace characters..*
: Matches any character (except for line terminators) zero or more times.\s+SET
: Matches one or more whitespace characters followed by the wordSET
.
Example 5: Find table names in DDL statements
You want to extract table names from CREATE TABLE
statements in your T-SQL scripts.
CREATE\s+TABLE\s+(\w+)
Example 5 explanation
CREATE\s+TABLE\s+
: Matches the wordsCREATE TABLE
followed by one or more whitespace characters.(\w+)
: Matches one or more word characters (alphanumeric and underscore) and captures them for extraction.
For more examples, visit Regular Expressions in Visual Studio