Skip to content

Latest commit

 

History

History
90 lines (63 loc) · 2.63 KB

File metadata and controls

90 lines (63 loc) · 2.63 KB
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
ignite-2025
dev_langs
TSQL
monikerRange =sql-server-ver17 || =sql-server-linux-ver17 || =azuresqldb-current || =azuresqldb-mi-current || =fabric-sqldb

REGEXP_COUNT (Transact-SQL)

[!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.

Arguments

string_expression

[!INCLUDE regexp-string-expression]

pattern_expression

[!INCLUDE regexp-pattern-expression]

start

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.

flags

[!INCLUDE regexp-flags-expression]

Return value

int

Examples

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;

Related content