Updated March 17, 2023
Definition of SQL REGEX
Regular expressions or SQL REGEX are the types of operations or a method to find a pattern in a string. In SQL, we can use regular expressions to find a particular string or pattern from the database. There is no need to install any package or a module to use the regular expressions in SQL; one could just use the term REGEX in the SQL query to implement the regular expressions. Here it is to be noted that the query here with regex is not case sensitive, and the backslash here is used to esca[e the particular character, so it is suggested to use a double backslash to find a particular pattern.
Key Takeaways
After carefully reading this article, one would be able to:
- Understand the core intuition of SQL queries and use regular expressions in the same.
- One would be able to understand the key concept of regular expressions and the need for the same.
- One would be able to implement regular expressions in SQL queries.
- One would be able to perform the pattern-matching task easily and efficiently with REGEX in SQL.
Syntax of SQL Regex
To Implement the regular expression in the SQL query, one needs to add the term “REGEXP” just above the regular expression.
For example, the syntax could look like:
SELECT name FROM student_table WHERE name REGEXP 'REGEX_Query';
Here as we can see in the above code, the SQL query with REGEX looks the same as the normal query, just an additional term REGEXP is used, and the regular expression is written just after that.
Listed Regular Expressions
[A-Z] | Matches the upper case letters |
[a-z] | Matches the lower case letters |
[0-9] | Matches any number between 0 to 9 |
[^abc] | Matches any character that is not listed in the bracket |
{n} | Matches n instances |
[[:<:]] | Matches the beginning of the words |
[[:>:]] | Matches the ending of the words |
* | Matches zero or all instances |
+ | Matches one or more instances. |
. | Matches any single character |
Need for SQL Regex
In a large dataset, there can be a need for pattern matching in the string to perform some of the search operations to get a part of the small for some important task. The pattern-matching task can be also done in normal SQL. Using SQL queries like LIKE or WHERE.
Using Regular Expressions in SQL queries makes the process of pattern searching more easy, fast, and more efficient Using SQL queries, pattern searching is a very easy task as compared to normal SQL queries, as some of the complex patterns can not be searched easily with normal SQL queries.
How to Work with SQL?
There are 3 ways to work with regex in SQL.
- LIKE
- SIMILAR TO
- POSIX
1. LIKE
LIKE is used to implement the Regex in SQL. LIKE operator is written just before the regex operations o execute the SQL query. It matches the condition and returns the value if true.
Code:
SELECT * FROM People
WHERE Name LIKE 'p%';
Here the above SQL query would return the name of the people starting with the character “p”.
2. SIMILAR TO
The SIMILAR TO operator is also the same as the LIKE operator, which matches the condition of the regex patterns and returns a value if the condition is True.
Code:
SELECT * FROM People
WHERE Name LIKE 'p%';
The above query would also returns all the names of people starting with the character “p”.
3. POSIX
The POSIX is different than the LIKE and SIMILAR TO Operators. Unlike the LIKE and SIMILAR to operators, first of all, POSIX is not an operator which is not written before the regex pattern.
As there are not any = and != in SQL, POSIX uses its own operators to command over regex, which is one of the most powerful techniques to use Regex in SQL.
Operators
- ~ : It is case-sensitive. It compares the two strings and returns true if the two strings are the same.
- !~ : It is case-sensitive. It also compares the two strings and returns false if the two strings are the same.
- ~*: It is not case-sensitive. It returns true if the first stirring is contained in the second string.
- !~*: It is not case-sensitive. It returns false if the first string is contained in the second string.
Examples
1. “Parth” ~ “P%” — True
“Parth” ~ “p%” — False (Case Sensitiveness)
2. “Parth” ~* “P%” — True
“Parth” ~* “p%” — True (Not Case-sensitive)
3. “Parth” !~ “P%” — False (It matches – returns false)
“Parth” !~ “p%” — True (Does not match – returns True)
4. “Parth” !~* “P%” — False (Matches – returns false)
“Parth” !~* “p%” — False (Matches — returns false)
SQL Regex Implementation
As we discussed above, the Regex can be implemented in the SQL query easily by adding the term REGEXP at the end of the query just before the pattern.
Syntax:
SELECT name FROM student_table WHERE name REGEXP 'Pattern';
Examples of SQL Regex
Following are the examples of SQL Regex with query:
1. Searching a String
SELECT title FROM websites WHERE title REGEXP 'eduCBA';
This query returns all the strings containing the word eduCBA in the title or a string.
Like, eduCBA writer, Course on eduCBA, etc.
2. Matching the End of the string
SELECT name FROM websites WHERE name REGEXP 'eduCBA';
This query returns the strings or sentences that are ending with the word eduCBA.
For Example, a Data Science course on eduCBA.
3. Matching the beginning of the string
SELECT name FROM websites WHERE name REGEXP '^eduCBA';
This query returns all the strings that are starting with the word eduCBA.
For example, eduCBA is a great platform for data science courses.
4. Matching the end of the words
SELECT title FROM websites WHERE REGEXP 'CBA[[:>:]]';
This query returns all the words that are ending with CBA.
For example, eduCBA.
5. Matching the start of the word
SELECT title FROM websites WHERE title REGEXP '[[:<:]]edu';
This query returns the words that are starting with edu.
For example, eduCBA.
6. Matching spaces and tabs
SELECT title FROM websites WHERE title REGEXP '[[:blank:]]';
This query matches all the blank spaces and tabs in the string in the document.
7. Matching number of characters
SELECT title FROM websites WHERE title REGEXP 'Ohk{3}';
This SQL query matches the number of characters that are listed in the curly bracket.
For example, this particular query will return all the matches with Ohkkk.
8. Matching all email addresses
SELECT email FROM data WHERE email REGEXP 'educba.com$';
This SQL query will return all the email addresses that end with educba.com
For example, [email protected]
9. Searching Keyword from a string
SELECT * FROM `name` WHERE `title` REGEXP 'eduCBA';
This SQL query returns the strings or a sentence that has the word “code” at the start, end, or in the middle of the sentence.
10. Alternative Search
SELECT * FROM `names` WHERE `title` REGEXP '^[ps]|^[i]';
This query returns all the names that are starting with either ps or i. The | vertical bar is used as an alternative here.
For Example, names like Parth, Ishan, etc.
11. Searching Multiple numbers of characters
SELECT * FROM `names` WHERE `title` REGEXP 'ok{2,4}';
This query will return all the matching string that is okk or okkkk.
Note that it will not return the string like ok or okkk.
Conclusion
In this article, we discussed the regular expression in SQL that can be used to make the pattern-matching process easier and more efficient. We discussed the brief idea of the Regex in SQL, with its core intuition, working mechanism, syntax, the need for it, and some of the examples of using and implementing the regular expressions in SQL. Knowledge about these concepts will help one to understand and use SQL queries on complex datasets easily. It will be able to perform well in tasks related to pattern matching in interviews.
Recommended Articles
We hope that this EDUCBA information on “SQL REGEX” was beneficial to you. You can view EDUCBA’s recommended articles for more information.