SQL Regular Expressions

0
163

sqltuts

SQL Regular Expressions

Following is the table of pattern, which can be used along with REGEXP operator in a query for a selected table in the database.

Pattern What the pattern is used /described for
^ The beginning of a given string
$ End of a given string
. Any single character you wish to take
[…] Any character   that has been listed between the square brackets
[^…] Any character that has not been listed between the square brackets
p1|p2|p3 Alternation; matches any of the patterns p1, p2, or p3
* Zero or more instances of preceding element you desired for
+ One or more instances of preceding element you desired for
{n} n instances of preceding element you desired for
{m,n} m through n instances of preceding element you desired for

Examples:

Query to find all the names starting with ‘ls’

 

Query to find all the names ending with ‘sh’

 

Query to find all the names, which contain ‘lok‘.

 

Query to find all the names starting with a vowel and ending with ‘rm’

 

 

[a-dX], [^a-dX]

Matches any character that is (or is not, if ^ is used) either a, b, c, d or X. A – Character between two other characters forms a range that matches all characters from the first character to the second. For example, [0-9] matches any decimal digit. To include a literal] character, it must immediately follow the opening bracket [. To include a literal – character, it must be written first or last. Any character that does not have a defined special meaning inside a [] pair matches only itself.

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.