SQL Regular Expression 

Sql-regex-feature.jpg

In database management, it is common to need to manage text and patterns. Using SQL regular expressions makes the process of searching, matching, and modifying the text information easy and efficient. They allow users to check the entries, clean up messy information, and extract valuable information from a large body of data. They are supported in database platforms like Oracle and MySQL. In this blog, you will understand what SQL regular expressions are, their types, with examples in detail.

Table of Contents:

What are Regular Expressions in SQL?

A SQL regular expression in a DBMS is a pattern that is used to search, match, or filter specific text in a database. It helps in finding the strings that follow a certain pattern, such as email addresses, phone numbers, or product codes. Regex is used to make pattern matching in SQL simple and powerful, so there is no need to write long and complex conditions.

A regex helps check whether a piece of text follows a rule, like a rule, such as ensuring that a username contains a combination of letters and numbers. They are widely used for data cleaning, text validation, and extracting useful information from large text columns.

SQL Regular Expression Syntax:

Regex in SQL utilizes pattern-matching operators to facilitate comparing a string with a specific pattern. Let us look at the basic syntax:

SELECT column_name 
FROM table_name 
WHERE column_name REGEXP 'pattern';

Explanation: Here, the REGEXP keyword is used to instruct the database to match the given pattern within the quotes.

Example: REGEXP ‘^[A-Z]’ will match any string that starts with a capital letter. 

Master SQL – Unlock Advanced SQL Skills Today
Master SQL – Unlock Advanced SQL Skills Today
quiz-icon

Importance of Regular Expressions in SQL Queries?

  1. Data Validation: A SQL regex is instrumental in validating whether the data is in the proper format. For example, it can check to see if an email or phone number is valid.
  2. Data Cleaning: Regex is used to find and delete extra characters, spaces, or symbols from text fields to keep your database neat.
  3. Pattern Extraction: Regex can extract components of text data, such as the part of an email representing the domain or the code from a product title.
  4. Flexible Searching: Rather than an exact match, regex allows you to search for patterns that follow some rules, which gives you more control.
  5. Text Replacement: With the help of functions like SQL replace regex, you can replace a specific text pattern without updating every record manually.

Types of Regular Expressions in SQL

Let us look at the main functions used in SQL regular expressions:

1. REGEXP_LIKE

The REGEXP_LIKE function in SQL checks whether a given value matches a pattern. It returns TRUE when the text matches the pattern and FALSE when it does not. This function helps filter records that are used to follow the specific text formats.

Syntax:

REGEXP_LIKE(column_name, 'pattern')

Example: Let us create an employees table and insert some data, which will be used to understand the types of regular expressions in SQL.

CREATE TABLE employees (
    emp_id INT,
    emp_name VARCHAR(50)

);

INSERT INTO employees (emp_id, emp_name) VALUES
(1, 'Sunil'),
(2, 'Sneha'),
(3, 'Ravi'),
(4, 'Sohan'),
(5, 'Anita');
-- using REGEXP_LIKE to fetch the employees whose name starts with 'S'
SELECT emp_name
FROM employees
WHERE REGEXP_LIKE(emp_name, '^S');

Output:

REGEXP_LIKE

Explanation: Here, the REGEXP_LIKE is used to fetch all the names that start with the letter “S”.

2. REGEXP_REPLACE

The REGEXP_REPLACE function is used to search for a text pattern and replace it with another string. It is widely used for cleaning or formatting data. This function is also known as the SQL regex replace function, as it helps to modify the strings directly based on a pattern.

Syntax:

REGEXP_REPLACE(string, 'pattern', 'replacement')

Example: 

CREATE TABLE customers (
    cust_id INT,
    address VARCHAR(100)
);
INSERT INTO customers (cust_id, address) VALUES
(1, 'Green Street #45'),
(2, 'Lake View-12'),
(3, 'Park Avenue@8');
-- Using REGEXP_REPLACE Function
SELECT REGEXP_REPLACE(address, '[^A-Za-z0-9]', '') AS clean_address
FROM customers;

Output:

REGEXP_REPLACE

Explanation: Here, the REGEXP_REPLACE function is used to remove all the special characters and spaces from the addresses to make it cleaner.

3. REGEXP_SUBSTR

The REGEXP_SUBSTR is a function in SQL that is used to extract a specific part of a string that matches a given pattern. This function helps in fetching the meaningful data, like domains, codes, or identifiers, from the longer text fields.

Syntax:

REGEXP_SUBSTR(string, 'pattern', start_position, occurrence, match_parameter)

Example:

CREATE TABLE users (
    user_id INT,
    email VARCHAR(100)

);
INSERT INTO users (user_id, email) VALUES
(1, '[email protected]'),
(2, '[email protected]'),
(3, '[email protected]'),
(4, '[email protected]');
SELECT REGEXP_SUBSTR(email, '@[^.]+') AS domain
FROM users;

Output:

REGEXP_SUBSTR

Explanation: Here, @[^.]+ is used to extract all the characters that come after “@”.

Get 100% Hike!

Master Most in Demand Skills Now!

Common Regex Patterns in SQL

Below are some of the most common regex patterns used in SQL.

Use Case Regex Pattern Description
Match alphabet-only usernames ‘^[A-Za-z]+$’ Matches strings that contain only letters
Match alphanumeric usernames ‘^[A-Za-z0-9_]+$’ Ensures the username contains letters, digits, or underscores
Match a valid email format ‘^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$’ Validates standard email address formats
Match numeric values ‘^[0-9]+$’ Matches strings containing only digits
Match phone numbers ‘^[0-9]{10}$’ Matches exactly 10 digits, useful for phone number validation
Extract domain part from an email ‘@[^.]+’ Captures the text immediately after “@”
Replace unwanted characters ‘[^A-Za-z0-9]’ Removes special characters from a string

Regex Examples in SQL Queries

Let us explore some of the practical examples that demonstrate the use of regex in SQL.

1. Match Alphanumeric Usernames

CREATE TABLE users (
    id INT,
    username VARCHAR(50)
);
INSERT INTO users VALUES
(1, 'Yash_1608'),
(2, 'RiyaGupta'),
(3, 'Amit@123'),
(4, 'Rohan#'),
(5, 'Simran');
-- Fetch usernames with only letters, numbers, and underscores
SELECT username
FROM users
WHERE username REGEXP '^[A-Za-z0-9_]+$';

Output:

Match Alphanumeric Usernames

Explanation: Here, this regex is used to check for usernames that are valid and contain only letters, digits, and underscores. Such expressions are useful for validating usernames during data entry or migration.

2. Validate Email and Phone Numbers

CREATE TABLE contacts (
    id INT,
    email VARCHAR(100),
    phone VARCHAR(20)

);

INSERT INTO contacts VALUES
(1, '[email protected]', '9876543210'),
(2, 'riya#mail.com', '12345678'),
(3, '[email protected]', '9999999999'),
(4, '[email protected]', '9876500000');
-- Fetch valid emails
SELECT email 
FROM contacts 
WHERE email REGEXP '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$';
-- Fetch valid phone numbers
SELECT phone 
FROM contacts 
WHERE phone REGEXP '^[0-9]{10}$';

Output:

Validate Email and Phone Numbers

Explanation: Here, this regex function is used to validate email and phone numbers with the help of SQL regular expressions.

SQL Regex in Different Databases

The implementation of SQL regular expressions differs among database systems, as each system provides its own syntax and functions to implement regex functionality. Let us look at how a few of the most frequently used databases implement regex, such as MySQL, SQL Server, and Oracle SQL.

1. Regular Expression in MySQL

MySQL supports regex using the REGEXP and REGEXP_REPLACE functions, which are widely used to match or clean the text patterns directly within queries.

CREATE TABLE customers (
    id INT,
    name VARCHAR(50)
);

INSERT INTO customers VALUES
(1, 'Yash Vardhan'),
(2, 'Riya 123'),
(3, 'Amit@Scaler'),
(4, 'Simran');
-- Find names that contain only letters and spaces
SELECT name 
FROM customers 
WHERE name REGEXP '^[A-Za-z ]+$';

Output:

Regular Expression in MySQL

Explanation: Here, this example is used to filter out the names that include digits or special symbols. MySQL regex functions are efficient for validation of data and cleaning tasks.

2. SQL Server Regex Usage

SQL Server does not support a direct regex function like MySQL, but we can use CLR integration or PATINDEX with pattern matching.

Note: PATINDEX only supports simple wildcards, not full regular expressions (regex).

-- Example using PATINDEX to find names starting with 'A'
CREATE TABLE employees (
    id INT,
    emp_name VARCHAR(50)
);

INSERT INTO employees VALUES
(1, 'Amit'),
(2, 'Riya'),
(3, 'Ananya'),
(4, 'Simran');
SELECT emp_name
FROM employees
WHERE PATINDEX('A%', emp_name) > 0;

Output:

SQL Server Regex Usage

Explanation: The PATINDEX function is used here, as SQL Server does not support the regex function directly, which is useful for simple pattern-based searches.

3. Oracle SQL Regex Functions

Oracle greatly enables the usage of regular expressions in SQL with built-in functions such as REGEXP_LIKE, REGEXP_REPLACE, REGEXP_SUBSTR, and REGEXP_INSTR. When you need to clean, extract, or validate data, it’s easy to use the Oracle SQL regular expression features.

CREATE TABLE users (
    id INT,
    email VARCHAR(100)
);
INSERT INTO users VALUES
(1, '[email protected]'),
(2, 'invalid_email.com'),
(3, '[email protected]');
-- Select valid emails
SELECT email
FROM users
WHERE REGEXP_LIKE(email, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');

Output:

 Oracle SQL Regex Functions

Explanation: Here, this query uses an Oracle SQL regular expression, which is used to validate the email addresses effectively.

Best Practices for SQL Regular Expressions

  1. Keep Patterns Simple: Do not use complex regex functions. Try to use simple functions as they run faster and are easier to maintain.
  2. Use Anchors: Apply ^ and $ for perfect matches instead of partial ones.
  3. Escape Special Characters: Always escape special characters (like ? or .) to prevent unintended matches.
  4. Limit Regex on Large Data: To improve the performance of the query, filter the data.

Common Mistakes to Avoid in SQL Regex

  1. Using Regex for Overly Complex Patterns: Use simple regex wherever you can to keep your regex queries fast and simple to read. 
  2. Missing Anchor Characters: Always use ^ and $ in the right locations when you require exact matches.
  3. Case Sensitivity Issues: Check the case behavior of the database or use (?i) in the Oracle SQL regular expression for case-insensitive matches.
  4. Unescaped Special Characters: Make sure to escape special characters (., ?) to avoid the regex making incorrect matches.
  5. Performance with Large Tables: Do not use regex on a large dataset. Filter your data out first to make it faster.

Real-World Use Cases of SQL Regular Expressions

  1. Email and Phone Validation: This is used to ensure that input data, such as email addresses or phone numbers, are of a recognized format before they are saved into a database. 
  2. Data Cleaning and Standardization: This is helpful to remove extra characters, whitespace, or symbols from fields such as names, addresses, or product identifiers to increase uniformity. 
  3. Extracting Specific Information: This provides the ability to extract useful data such as domain names from URLs, order IDs from text, or dates from unstructured logs. 
  4. Masking Sensitive Data: This is used to either hide or partially replace confidential information, such as credit card numbers or similar sensitive details, for security.
SQL Simplified: Learn for Free, Grow for Life
Master the language of data and start your journey toward a data-centric career—without spending a dime!
quiz-icon

Conclusion

Regular expressions in SQL are a very effective way to search, clean, and extract text in your databases, and they can also simplify data validation, formatting, and transformation. Whether you use SQL regular expressions or Oracle SQL regular expressions, the accuracy and speed of handling data will improve. Understanding and implementing regex with SQL will add value in your career as a professional because you will be able to maintain clean, structured, and efficient databases with a real-world applicable nature. 

Take your skills to the next level by enrolling in the SQL Training Course today and gaining hands-on experience. Also, prepare for job interviews with our SQL Interview Questions, prepared by industry experts. 

SQL Regular Expression – FAQs

Q1. Can regex in SQL be case-insensitive?

Yes, use (?i) in Oracle for case-insensitive matches. In MySQL, regex is case-insensitive by default.

Q2. How is REGEXP different from LIKE?

LIKE is for simple patterns, while REGEXP handles complex ones like email or phone validation.

Q3. Does regex affect query performance?

Yes, regex can slow queries on large data. Use it with filters or smaller datasets for better speed.

Q4. Can regex be used with other SQL functions?

Yes, you can combine regex with functions like CASE or SUBSTRING for dynamic validation and formatting.

Q5. Do all databases support regex?

No. MySQL, Oracle, and PostgreSQL support it natively, but SQL Server needs workarounds like CLR or PATINDEX

About the Author

Technical Writer | Business Analyst

Yash Vardhan Gupta is an expert in data and business analysis, skilled at turning complex data into clear and actionable insights. He works with tools like Power BI, Tableau, SQL, and Markdown to develop effective documentation, including SRS and BRD. He helps teams interpret data, make informed decisions, and drive better business outcomes. He is also passionate about sharing his expertise in a simple and understandable way to help others learn and apply it effectively.

business intelligence professional