Skip to main content

Regular Expression in Oracle SQL

Regular Expression in Oracle SQL

Regular Expression in Oracle SQL is a pattern matching technique to retrieve sort of data based on the conditions given in Oracle database.

Pattern Matching is similar to functions like, replace, substring and in string which are previously seen topics.

At the end of this article there will be certain task to practice for this Regular Expressions. It is very important to be very clear about this topic, this are very helpful for many interview Questions.

Regular expression in Oracle SQL

Using regular expressions with Oracle Database: Overview

Regular Expression is one of the easiest way to learn with clear understanding if one are familiar with the below
  • Basic string matching patterns,
  • Quantifiers,
  • Meta characters,
  • Functions etc.
Note: The above points should understand clearly means it will be very easy to write a query.

Basic String Matching with Regular Expressions

|

Either or 

()

Group

\

Escape

^

Start

$

Stop


Literals of regular expression in oracle sql


[ a b c ]

a, b, c

[^ a b c]

Any character except a, b, c

[ a - z ]

a to z all characters

[ A - Z ]

A to Z all characters

[ a- z A-Z]

All letters small or caps

[ 0 - 9]

Numbers between 0 to 9


Quantifiers used in Regular Expression of SQL

[ ] ?

Occurs 0(zero) or 1 times

[ ] +

Occurs 1 or more times

[ ] *

Occurs 0 or more times

[ ] {n}

Occurs n times

[ ] {n, }

Occurs n or more times

[ ] {y, z}

Occurs at least y times but less than z times

Meta characters in Regular Expression of SQL 

\d

[0-9]

\D

[^0-9]

\w

[a-z A-Z 0-9]

\W

[^\w ]

Oracle Database SQL Functions for Regular Expressions

In Oracle Database SQL functions consists of 5 functions in regular expressions such as:-
  1. REGEXP_LIKE
  2. REGEXP_SUBSTR
  3. REGEXP_INSTR
  4. REGEXP_REPLACE
  5. REGEXP_COUNT
This are the five most important regex pattern matching expressions are going to see detail with examples and explanations 

REGEXP_LIKE

Like operator is used to get the record for the giving pattern.

This Regexp_like can be used in Where condition not in SELECT clause.

Let's Understand some basic match parameters which are helps to get the record based on our input string.

‘i’

Case insensitive Matching

‘c’

Case sensitive Matching

‘n’

Allows the period  (.), to match new line character

‘m’

The source string will be in multiple line



End of the Studying Part.

If you completed reading the above tables or charts, is only enough to retrieve the query 

From the above information, now we are going to workout all the patterns with examples

regular expression in like function







We will use this table for data retrieving.

Table Name : detail

Script for the above table

----- Creating a table detail


CREATE TABLE detail

       (id NUMBER

       ,Name VARCHAR2(20)

       ,email VARCHAR2(50)

       ,Amount NUMBER

       ,Contact VARCHAR2(15)

       );


------ Inserting a record in a table

    

BEGIN

       INSERT INTO detail VALUES (1000, 'Kaviarasan', 'kavi.777@gmail.com', 50000, '+91 90000 0000');


       INSERT INTO detail VALUES (2300, 'karthi', 'karthi.111@gmail.com', 90000, '+1 90008 0000');


       INSERT INTO detail VALUES (8900, 'mohan', 'mohan4444@yahoo.com', 55000, '+10 90000 0000');


       INSERT INTO detail VALUES (1070, 'Bill', 'bill.kevin@gmail.com', 500000, '+044 23000 0000');


       INSERT INTO detail VALUES (0300, 'Rahul', '7777rahul@gmail.com', 200000, '+28 90500 0000');


       INSERT INTO detail VALUES (0020, 'rekha', 'rek18son@gmail.com', 40000, '+91 89000 0000');


     COMMIT;


END;

/


REGEXP_LIKE patterns with examples

Basic Syntax : REGEXP_LIKE (source string, pattern)

1, Print the Name which starts with letter 'k' it may be any cases (Upper or lower)

regular expression like function example 1

^  which is used to denote starting with letter (k)

The above query only print Lower case "k" but not prints Upper case "K".

Similarly instead of Lower "k" if we give "K" means it will print upper case name only.

To print all (K and k) we need to use (i) which mentions case in sensitive matching.

regular expression like function example 2














2) Print the name which contains "a" 

regular expression like function example 3

From the above query the records which are fetched are single "a" Name.

{1} it is a quantifiers used to mention the n times.

WHERE REGEXP_LIKE (NAME, '[a]{2}') => it is used to print two continuous "a"
 Example: aarthy, raam, aakash, saami, etc..

Same as for three, four etc...

3) Print the name that must contain the letter (r, s, t, u, v, w, x, y, z)

regular expression like function example 4





[r-z] patterns used to print the letter from r to z of all characters (literals in regular expression).

4) Print the name that must contain all character such as "akr"
 and write another query to print any of the name should contain this any one character "akr"

regular expression like function example 5

5) Print the name that should starts with "m" and ends with "n"

SELECT name FROM detail
WHERE REGEXP_LIKE (name, '^m[a-z]+n$');

^m  used to define letter starts with m
[a-z] next letter will be between a to z
+ denotes 1 or more times
$ means ends

6) The name that should ends with vowels (a, e, i, o, u)

SELECT * FROM detail
WHERE REGEXP_LIKE (Name, '[a-z]+(a|e|i|o|u)$');

() -  group
| - used to identify either or 
$ - used to end the word

7) Find the Gmail user and yahoo user in the table
regular expressin like function example 6
It's easy to get the users by using regular expression, by giving directly in the pattern.

8) Print the ID and NAME that contains first letter should be between (a) to (z)

regular expressin like function example 7












CONCLUSION

I hope you got some clear understanding about the basic patterns to regular expression in oracle SQL, and as well as regexp_like functions.

This is the first part of the regular expression functions and other 4 functions are explained in next 2 parts.

If anyone having any queries or doubts means kindly reach out us through official mail or comment below.


Comments

Popular posts from this blog

Oracle SQL Interview Questions Series 1 [Basic]

Oracle SQL Interview Questions Series - 1 Oracle SQL Interview Questions covered in Eduflee, so complete all the series to get placed into the IT company. At present Structured Query Language is very important to enter into the Software company. After completing all the Interview series, you can crack many interviews with 100% assurance from my experience. Basic Questions for SQL interview Schema => User Records => Rows Fields => Columns Result set => Output 1, What are the Schema Objects in Oracle SQL ?     Table, View, Materialized view, Sequence, Synonyms, Index are the schema objects in SQL.     Table => Basic Unit of Storage [Rows and Columns]     View => Logical Representation of subject of records, It does not contains any data. So here data is logical.     Materialized View =>   It contains the data, it is also called as Snapshot. It contains data physically.     Sequence => It is a Numeric Value Generator     Synonyms => used to provide alternate nam

Class 3 Functions in Oracle SQL

Functions in Oracle SQL Functions is one of the important features in Oracle SQL, with the help of this functions we can able to get the result as per our requirement. Functions in Oracle SQL contains Single Row Functions, Multiple Row Functions. Mainly those functions are used to returns value based on the input that which we given. And practicing this functions in oracle SQL, one can able to master in query writing. Functions is used to perform some actions like doing any calculations, grouping the data, conversion in type, date conversion etc. Types of Functions in Oracle SQL Functions in oracle is classified on the basis of the input which we are giving and getting the output, and they are: - Single Row Functions - Multiple Row Functions Single Row Functions in Oracle SQL Single Row Functions works on the single rows which will shows the result for that single rows only. If "n" number of inputs to give "n" number of outputs. From the above figure explains about

Learn SQL From Scratch to Advanced topics

Learn SQL from Scratch to Advanced  Nowadays, many IT company moreover depend on SQL  only. So this is one of the basic query language to start for new developers. So track or to be master in SQL, the below topics should be covered. List of Basic Topics :- 1, Should know what is frontend and backend developer  2, What is DBMS, RDBMS ? 3, What is SQL ? 4, What are the database are there ? Beginner Level SQL topics  This are some beginner level SQL topics, if you are new to SQL this are the topics that should be covered first. 1, Oracle Basics  2, List of Objects in SQL   3 , Clause and its usage   4, Functions  5, Sub Query  6, Joins  7, Decode and Case 8, Constraints 9, Important Data Dictionary Table like user_source, all_source, user_tables etc.., Intermediate Level SQL topics (Creating Objects)  When we are entering into objects this below topics should be covered. In  this topics that we are creating database objects like tables, views, sequences, synonyms etc. 1, Data Definitio