Advent of 2025, Day 6 – SQL Server 2025 – New T-SQL functions – REGEXP_LIKE()

In this Microsoft SQL Server 2025 series:

  1. Dec 01: Microsoft SQL Server 2025 – Introduction and installation
  2. Dec 02: Microsoft SQL Server 2025 – New T-SQL functions – native JSON data type and some functions
  3. Dec 03: Microsoft SQL Server 2025 – New T-SQL functions – JSON Index
  4. Dec 04: Microsoft SQL Server 2025 – New T-SQL functions – Product()
  5. Dec 05: Microsoft SQL Server 2025 – New T-SQL functions – BASE64_ENCODE() and BASE64_DECODE()

SQL Server 2025 introduces a new T-SQL functions for Regular Expressions (RegEx).

With multiple RegEx functions, the LIKE function indicates if the regular expression pattern matches the string or is in a string. The function is REGEXP_LIKE() that will do the job.

In order to run this, you have to have your SQL Server 2025 database set to compatibility level 170.

USE master;
GO
DROP DATABASE IF EXISTS db_05_regex;
GO
CREATE DATABASE db_05_regex;
GO

-- Just in case
ALTER DATABASE db_05_regex SET COMPATIBILITY_LEVEL = 170;

USE db_05_regex;
GO

Using REGEXP_LIKE with CREATE TABLE

You can use the REGEXP_LIKE for check in create table statement. And it will evaluate and checked with every row inserted into the table.

DROP TABLE IF EXISTS EMPLOYEES;
GO
CREATE TABLE EMPLOYEES (  
    ID INT IDENTITY(101,1),  
    [Name] VARCHAR(150),  
    Email VARCHAR(320)  
    CHECK (REGEXP_LIKE(Email, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')),  
    Phone_Number NVARCHAR(20)  
    CHECK (REGEXP_LIKE(Phone_Number, '^\(\d{3}\) \d{3}-\d{4}$'))  
)

And so with the valid or invalid insert, you will get messages accordingly.

-- Valid INSERT
INSERT INTO EMPLOYEES ([Name], Email, Phone_Number)
VALUES ('Tomaz Kastrun', 'tomaz.kastrun@example.com', '(123) 456-7890');
GO

And you will have 1 row inserted, because the email and the phone number follow the correct format. With invalid phone number or email, your inserted will be be executed, since it will have a conflict with the CHECK constraint on phone_number field.

-- Invalid INSERT
INSERT INTO EMPLOYEES ([Name], Email, Phone_Number)
VALUES ('Tom Jones', 'tom.jones@example.com', '123-456-7890');
GO

Using REGEXP_LIKE with SELECT statement

With select statement the REGEXP_LIKE handles the string Expression, the pattern expression and flags.

-- SELECT
SELECT * FROM EMPLOYEES
WHERE REGEXP_LIKE(Email, '^[^@]+\.[^.]*exa.*\.com$');
GO

If you want yo enchance the accuracy of the CE (Cardinality estimator) for the REGEXP_LIKE function, Microsoft suggests to use the ASSUME_FIXED_MIN_SELECTIVITY_FOR_REGEXP and ASSUME_FIXED_MAX_SELECTIVITY_FOR_REGEXP query hints.

Tomorrow we will look into the new T-SQL functions for REGEXP_SUBSTR and REGEXP_REPLACE.

As always, the code is available at my Github: https://github.com/tomaztk/SQLServer2025

Happy coding!

Tagged with: , , , , , , ,
Posted in SQL Server
18 comments on “Advent of 2025, Day 6 – SQL Server 2025 – New T-SQL functions – REGEXP_LIKE()
  1. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  2. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  3. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  4. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  5. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  6. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  7. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  8. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  9. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  10. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  11. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  12. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  13. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

  14. […] Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE() […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, Fabric, enterprise Power BI, SQL Server BI, Data Modeling, SSAS Design, SSRS, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started