| title | REGEXP_COUNT (Transact-SQL) | |
|---|---|---|
| description | REGEXP_COUNT returns the number of times that a matched expression pattern exists in a string. | |
| author | MikeRayMSFT | |
| ms.author | mikeray | |
| ms.reviewer | abhtiwar, wiassaf, randolphwest | |
| ms.date | 11/18/2025 | |
| ms.service | sql | |
| ms.subservice | t-sql | |
| ms.topic | reference | |
| ms.custom |
|
|
| dev_langs |
|
|
| monikerRange | =sql-server-ver17 || =sql-server-linux-ver17 || =azuresqldb-current || =azuresqldb-mi-current || =fabric-sqldb |
[!INCLUDE sqlserver2025-asdb-asmi-fabricsqldb]
Counts the number of times that a regular expression pattern is matched in a string.
REGEXP_COUNT
(
string_expression,
pattern_expression [ , start [ , flags ] ]
)
Note
Regular expressions are available in Azure SQL Managed Instance with the SQL Server 2025 or Always-up-to-date update policy.
[!INCLUDE regexp-string-expression]
[!INCLUDE regexp-pattern-expression]
Specify the starting position for the search within the search string. Optional. Type is int or bigint.
The numbering is 1-based, meaning the first character in the expression is 1 and the value must be >= 1. If the start expression is less than 1, the returned pattern_expression begins at the first character that is specified in string_expression. If the start expression is greater than the length of string_expression, the function returns 0. The default is 1.
If the start expression is less than 1, the query returns an error.
[!INCLUDE regexp-flags-expression]
int
Count how many times the letter a appears in each product name.
SELECT PRODUCT_NAME,
REGEXP_COUNT(PRODUCT_NAME, 'a') AS A_COUNT
FROM PRODUCTS;Count how many products have a name that ends with ing.
SELECT COUNT(*)
FROM PRODUCTS
WHERE REGEXP_COUNT(PRODUCT_NAME, 'ing$') > 0;Count how many products have a name that contains three consecutive consonants, ignoring case.
SELECT COUNT(*)
FROM PRODUCTS
WHERE REGEXP_COUNT(PRODUCT_NAME, '[^aeiou]{3}', 1, 'i') > 0;