Oracle 19 C (SQL/PLSQL) DURGA SOFT
bamole
Sql> insert into emplempno} values(1);
‘Sql> update emp set salzsal+100 where ename= ‘SMITH’;
Sql> savepoint si;
SSqb> insert into emplempno) values(2);
Sql> update emp set sal=sal+100 where ename= ‘KING;
Sql> savepoint £2;
SqP delete trom emp wnere empr
Sql> rollback to 51;
SSql> commit;
SSqb> select * from emp;
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. zk
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
Oracle 10g introduced regulor expressions. Regular expressions are special type of notation which is used
to search/mateh/replace strings. Regular expressions are also called posix notations.
"Note: In oracle regular expression works with CHAR, VARCHAR, VARCHAR2, CLOB datatypes but regular
‘expressions doesn’t work with LONG datatype. Regular expression having so many Meta characters
through these Meta characters we'can search or match or replace'strings. Meta characters ate special
characters having special meaning.
Oracle having following pre-defined regular expression functions. These ar
3) Regexp_tike()
2) Regexp instr)
3) Regexp_substr()
4) Regexp_replacel)
5) Regexp_count() (oracle 12g)
Matches any single character.
Matches one or more occurances of preceding char.
Matches zero or more occurances of preceding char.
Matches zero or one occurances of preceding char.
Begening of fine anchar.
End of line anchar.
Escape char.
Matches any char in the fist.
"Negation > Le matches other than list of characters.
OR->alb-> matches either aor.
=
VUGVVYY EY eUUY
{mp Matches exactly m occurances.
{m,n} Matches at least m to n occurances.
{Gupper:]] ‘Matches upper case characters.
[bower] Matches lower case characters.
[alpha:]] Matches alphabets.
\d > Matches digs
\s > Matches spaces
Ww > Matches word
Backrefarences->ie
\n > \L> represent first subgroup.
\2> represent second subgroup.
{ud > Subexpression or grouping.
1} Regexp_like() : This function is used to search strings based on regular expression pattern,
“Syntax: regexp_ike coleame, ‘pattern’
4. Write a query to display the employee whose ename contain ‘AM? (or) ‘AR’ string fram emp table by
using lke operator?
‘Ans: select * from emp where ename like %AMS% or ename lke ‘KAR’;
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. kw
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
A White a query to display the employee whose ename contain AM or AR strings from emp table by
using regexp._like() ?
‘Ans: select * from emp where regexp_tke (ename, ‘AM|AR');
‘Ewewo | ewawe 300 Mon HIREDATE 3a
[752 "WARD | SALESMAN | 7698 22-FEB-81 1250,
7654 | MARTIN | SALESMAN | 7698 28-SEP-82 1250
7782 CLARK | MANAGER | 7839 coJUN-s1_| 2450
7876 ‘ADAMS ‘CLERK. 7788 __|_23-MAY-87 | 1100
"7900 James | CLERK 7638 | _03-DEC-81 950
4. Write a query to display the employee whose ename starts with either A or B from emp table by
using regexp_like() ?
5 select * from emo where regexp_like (ename, *A/8'
Ewpno_[Tewame [7108 man “| winEDATE [SAL
7499 | allen | Salesman | 7598 | 20Fe6-63 | 1600
7608 | BLAKE | Manacer | 7439 | oxmavex | 2as0
7a6__| ADAMS | _cuenx | 778 | za-mave7 | 1100
A. White a query to display the employee whose ename end with either N or S from emp table by using
regexp_like )?
‘Ans: select * from emp where regexp_like (enome, ‘NS/S$';
A Write a query to display the employee whose ename 2” letter is either M or Lor A from emp table by
using regexp like () ?
‘Ans: select * from emp where regexp_l
celename,*-[MLAT};
A. White a query ta display the employees whose ename 2~ letters nota ML A rom emp table by
using regesp kel)?
‘Ans: SQL> select * from emp where regexp_like(ename, “®[*MLAY';
‘Note: regexp_ike() having optional 3" parameter this 3" parameter is a match optional. This match option
nas 2 Meta characters.
1. C (Case sensitive) > (Defautt)
1 (Caseinsensitive)
‘Syntaxc regexp_lke (colname, ‘pattern’, [match option!)
A. write a query to display the employees whose enome starts with $ from emp table by using
regexp_tikel)?
‘Ans: SQL> insert into empfempro, ename) values(, smith’),
SQL> select * from emp;
‘SQL> select ename from emp where regeap_like(ename, *S');
‘ENAME
‘SMITH
SCOTT
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. zm
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
SQL> select * from emp where regexp.
fename, *S!,C);
SQL> select * from emp where regexp_tike{ename, AS, ');
‘SOL> create table test (phoneno varchar2(20));
‘SQL> insert into test values('123.456.789.4579');
‘SQL> insert into test values('345.567.985');
‘SQL> insert into test values('345.567.985.35));
SQL> commit;
‘SOL> select * from test;
Phaneno
123, 456.789.4575
'345.567.985
345.567 985.35
4 white @ query to dsply those phonene hevng 3 digits digs .end with 3 digs fom the obove
table by using regexp kel)?
‘Ans: SQL> select * from test where regexp_ike(phoneno, \d(3} \d(2}.\d(3));
Phoneno
173.456.709.457
'345,567.985
345 567 585.35
Solution
‘SQL> select * from test where regexp_like{phoneno, “\i(3). \d(3)\d(3) $1);
for)
‘SQL> select * from test where regexp_like(phone,‘I:dight]{3}{I:dight:]13}.[:igit]}3} $":
Phoneno
345,567,985
Examole
‘Ql> erate table test2lenamne,varchar{20)
‘SQL> insert into test] values(‘aaeabec’);
SQL> inser into test values 2abhbbe)
SQL> insert into test1 values(‘aacc’};
‘SQL> insert into test1 values(’a’);
SQL commit
SQL> select * from test;
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. Ea
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
ENAME
aazabec
‘abbbbe
eae
A” Write a query to display the employees whose ename having at least second ‘a’ and ero or more ‘t’
‘ond at least one “’ from the above table by using regexp_hke() ?
‘Ans: SQL> select * from test regexp_lke(ename, ‘satb*ct"
ENAME
aoaabec
‘zabbbbe.
ace
Note: f you want to escap spectal mearing ofthe meta character then we must use \( bachward ) infront
ofthe meta character.
SQL> ingert into testa values (*'};
Lrow created.
SQL> select * from test;
ENAME
soaabec
aabbbbe
ace
SQL> select * from test1 where regexp_like (ename, \"};
ENAME
SQL> create table test2;
SOL> Insert into test2 values’. abc’);
SQL> insert into test2 values('2. abc’;
SQL> insert into test2 values(’3._ abc’);
‘SQL> insert into test2 valuest’4.abe");
ENAME
Tabe
2. abe
3. abe 7
“abe
A Write a query to display the employees whose ename having following regular expression data by
using above table “one digit” “one or more spaces abe”?
‘Ans: SQL> select * from test2 where regexp_tikeltest2, \d\.\stabe
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. kw
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
2), Regexp inst):
‘his function always returns number of datatype. This function returns postion based on specified
regular expression pattern, it is also same as either INSTRQ. This function accepte accepts S
Parameters. This function is also same as INSTR() but this function having an optional S® parameter.
‘his parameter represents return position, ths retum position having 2 values either OOF
‘Syntax: regexp_instr(colname/string, ‘pattern’, searching position, no of occurrences from position, return
Position
Return position {00° 2)
: (Default current potition)
lext position of regular expression pattern)
Sample:
SOL> select egexp_instr‘ABCADEF, [i
oyp4 :
sa1> 2
oyp:a
SOL> select egexp_inste‘ABCIDEF’, digit I.1.4,) from duak,
opps
it]!',1.1) from dual;
51,40) frorn duals
Bxamote
SOL> create table testimalid varchar2120)
SQL> insert into test values(‘abe@gmail.com’);
SQL> insert into test values(‘xy2@ gmait com);
SQL> insert into test values{‘par @zmail.com’);
SQL> insert into test values(‘zzz @gmailcorn);
SQL> select * from test;
‘MAID
abe@gmailcom
ay@emall. comm
ar @gmailcom
zz Ggmailom
A. White a query to display valid mail id from the above table by using regexp_Instr()?
Ans: SQL> select * from test where regexp_instr{maild, “@")>0 and regexp_inste(maiid,*
MAID
‘abc@gmailcom
»ye@gmail. con”
3) Regexp_substr()
This function will extract portion ofthe string within given string based on regular expression pattern,
itis also same cs SUBSTR().
Example:
SQL> select regexp_substr‘oraclet12c, [0-9}+) from dual;
oye :12
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. zk
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecomOracle 19 C (SQL/PLSQL) DURGA SOFT
SQL> select regexp_substr{‘oracle32e,, 10-9}. from dual;
oy: 12
‘SQL> select segexp_substr('100 abe xyz’, “\w+".13) from dual;
O/P: xyz
SQL> select regexp_substr{'100 abe xyz, \w',2,2) from dual;
O7P:abe
4) Regexp_replace(): This function Is used to replaces character by string by stringy using regular
expression pattern.
Sample:
‘SQL> select regexp_replace(‘abcinyz’ , TE:digit J), ‘oracte’) from dual;
o/P: abeoradenre
Backreference and grouping
In oracle if you want to divide string into some group then we must specified group of characters within
Paranthesis and also these subgroup are control by using back reference. In regular expression back
feferences is represented by using \n.Here n represented number, here \l represents frst group and also
\2 represent second group.
[[Svntax: reexp_replace(colname/string,\groupi)(group2)....,backreference’)
Sample:
SOL> seloct regoxp_roplace(‘oraciel2e, {(:apha:}}+)(Caigit}+.), \1\2) from dual;
/?: oracle 12¢
SQL> select regexpreplace(‘oradlel2e, Ukalphai}+)( [digit ]}+.), “\2\X) from dual
O/P:12corade
'5) Regexp_count():This function always return no. data type. This function count no. of occurances of the
specified character based on regular expression pattern,
‘Syntax: regexp_count (columnname, ‘pattern’
Sample:
SOL> select regexp_count(‘abelxyzipqr1’, TEsigit I") from dual,
Output: 3
Uke operator escape function;
Im oracte lke operatorhaving 2 special characters, these are:
Lb o%
ji, _(underscore}
‘These special characters are also called as wild card character. These special characters have special
reaning. Whenever these wid card characters are available within a table column and alo when we are
tty ta retrieve database on these wild card character, then oracle server returns wrong result
‘To overcome this problem for escaping these wild card character special meaning, then ANSI/ISO SQL.
provided escape function, this escape function used along with lke operator only.
‘Syntax: select * from tablename where colname like ‘patter? escape ‘escape character's
Here escape character length must be 1 byte.
DURGA SOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500 038. Eu
Phone | 72 07 21 24 27, 80 96 96 96 96, 92 42 21 21 43 | www.durgasoftonlinecom