SlideShare a Scribd company logo
SQL Regular Expression
A regular expression (regex or regexp for short) is a
special text string for describing a search pattern. You
can think of regular expressions as wildcards on
steroids. You are probably familiar with wildcard
notations such as *.txt to find all text files in a file
manager. The regex equivalent is ^.*.txt$. (Definition
from http://www.regular-expressions.info/).
SQL Regular Expression
There are many ways to use this as some example show on the internet, however I will use
an example which I was challenged with to ilustrate how interesting it could be to use
Regular Expression in SQL. I was asked to get all records from a table which C(50) field
contain the below pattern anywhere in the field.
Pattern 1: I###### 'I [0-9]{5}‘ character ‘I’ follow by 6 digits
Pattern 2: I ##### 'I [0-9]{5}‘ character ‘I’ follow by space and then by
6 digits
Pattern 3: IMP##### IMP [0-9]{5}‘ character IMP follow by 6 digits.
Pattern 4: IMP ##### ‘I-[0-9]{5}‘ character IMP follow by space and then by
6 digits.
Pattern 5: ###-#######-# [0-9]{3}-[0-9]{7}-[0-9]{1,} 3 digits, then dash, then 7 digits, then
1 digit and exact just one digits at the end, no more than one.
SQL Regular Expression
• 1). Select
• 2). PLDESC,
• 3). IFNULL(REGEXP_SUBSTR(PLDESC,'I [0-9]{5}'),'N'),
• 4). IFNULL(REGEXP_SUBSTR(PLDESC,'IMP [0-9]{5}'),'N'),
• 5). IFNULL(REGEXP_SUBSTR(PLDESC,'I-[0-9]{5}'),'N'),
• 6). IFNULL(REGEXP_SUBSTR(PLDESC,'IMP[0-9]{5}'),'N'),
• 7). IFNULL(REGEXP_SUBSTR(PLDESC,'I[0-9]{5}'),'N'),
• 8). IFNULL(REGEXP_SUBSTR(PLDESC,'[0-9]{3}-[0-9]{7}-[0-9]{1,}'),'N')
• 9). From apl
• 10). Where
• 11). REGEXP_like(PLDESC,'[0-9]{3}-[0-9]{7}-[0-9]{1}[^0-9]') Or
• 12). REGEXP_LIKE(PLDESC,'I [0-9]{5}') Or
• 13). REGEXP_LIKE(PLDESC,'IMP [0-9]{5}') Or
• 14). REGEXP_LIKE(PLDESC,'I-[0-9]{5}') Or
• 15). REGEXP_LIKE(PLDESC,'IMP[0-9]{5}') Or
• 16). REGEXP_LIKE(PLDESC,'I[0-9]{5}'
SQL Regular Expression
1). Sql select statement
2). Select Field PLDESC
3). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern
I 5 digits(I #####) (I [0-9]{5}') if null, means the string does not
have the pattern then instead of showing null (‘-’) show ‘N’
4). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern
IMP 5 digits(IMP #####)( Character IMP, follow by space and then
follow by 5 digits)
5). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern
I-5 digits(I--#####)( Character I-, follow by 5 digits) 'I-[0-9]{5}'
if null means the string does not have the pattern then instead of
Showing null (‘-’) show ‘N’.
6). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern
IMP 5 digits(IMP #####)( Character IMP, follow by space and then follow
by 5 digits)
IMP[0-9]{5} if null means the string does not have the pattern then
instead of Showing null (‘-’) show ‘N’.
7). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern
I5 digits(I#####)( Character I, follow by 5 digits) 'IMP[0-9]{5}'
if null means the string does not have the pattern then instead of
Showing null (‘-’) show ‘N’
SQL Regular Expression
8). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern ###-#######-# (3#-, dashes, 7#-, dashes, 1 or
more #)( 3 digits follow by dashes, 7digits follow by dashes and then 1 or more digits, Digits at the end no
character) ‘[ 0-9]{3}-[0-9]{7}-[0-9]{1,}' if null means the string does not have the pattern then instead of
showing null (‘-’) show ‘N’.
9). From table APL
10). Where Clause
11). String contain the pattern'[0-9]{3}-[0-9]{7}-[0-9]{1}[^0-9]' (###-#######-#) (3 numbers, dashes, 7 numbers,
dashes and one digit at the end no more than1). Pay Attention to this part [0-9]{1}[^0-9] that’s
means 1 digit ([0-9]{1}) and after this no more digits ([^0-9]) Or.
12). String contain the pattern ‘I [0-9]{5}' which means Character ‘I’ follow by space and then 5 digits Or
13). String contain the pattern 'IMP [0-9]{5}' which means Character ‘IMP’ follow by space and then 5 digits Or.
14). String contain the pattern ‘I-[0-9]{5}’ which means Character ‘I’ follow by dash and then 5 digits Or.
15). String contain the pattern 'IMP[0-9]{5}' which means Character ‘IMP’ follow by 5 digits Or.
16). String contain the pattern 'I[0-9]{5}’’ which means Character ‘I’ follow by 5 digits.
Note: this substring will be extracted it doesn’t matter in what position it exists in the string field PLDESC which is
C(50). Will be extracted From anywhere it exists in the string field.
There are other SQL REGEXP that maybe helpful in case you need it like
REGEXP_REPLACE (Replace a pattern of string)
REGEXP_INSTR (Give the position where a pattern of a string start)
REGEXP_MATCH_COUNT (Count occurrences of a pattern in a string)

More Related Content

What's hot (12)

PPTX
Bring sanity back to sql (advance sql)
Eyal Trabelsi
 
PDF
Constructs and techniques and their implementation in different languages
OliverYoung22
 
PPT
T03 a basicioprintf
teach4uin
 
PDF
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
PPT
Arrays and Pointers
SimoniShah6
 
PDF
Stack and Queue (brief)
Sanjay Saha
 
PDF
Introduction_modern_fortran_short
Nils van Velzen
 
PPT
Stack and queue
Katang Isip
 
PDF
Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
PDF
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
PDF
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Bring sanity back to sql (advance sql)
Eyal Trabelsi
 
Constructs and techniques and their implementation in different languages
OliverYoung22
 
T03 a basicioprintf
teach4uin
 
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
Arrays and Pointers
SimoniShah6
 
Stack and Queue (brief)
Sanjay Saha
 
Introduction_modern_fortran_short
Nils van Velzen
 
Stack and queue
Katang Isip
 
Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
http://algorithmtraining.com/advanced-python-training-hyderabad/
 

Similar to RPG Sql regular expression (20)

PPSX
Regular expressions in oracle
Logan Palanisamy
 
PDF
DEE 431 Introduction to MySql Slide 6
YOGESH SINGH
 
PPTX
SQL for pattern matching (Oracle 12c)
Logan Palanisamy
 
PPT
Les08
Sudharsan S
 
PPTX
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
PPTX
11. using regular expressions with oracle database
Amrit Kaur
 
PPTX
Advance sql session - strings
Eyal Trabelsi
 
PPT
Beg sql
KPNR Jan
 
PPT
Beg sql
Karthik Perumal
 
PPTX
regex_presentation.pptx
BeBetter4
 
PPTX
Intro to T-SQL – 2nd session
Medhat Dawoud
 
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
KEY
libinjection: new technique in detecting SQLi attacks, iSEC Partners Open Forum
Nick Galbreath
 
PPTX
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
PPT
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
PDF
New features in abap
Srihari J
 
PDF
A SAS<sup>®</sup> Users Guide to Regular Expressions When the Data Resi...
Ken Borowiak
 
PDF
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
DOCX
Assg2 b 19121033-converted
SUSHANTPHALKE2
 
PPTX
Structure Query Language (SQL).pptx
NalinaKumari2
 
Regular expressions in oracle
Logan Palanisamy
 
DEE 431 Introduction to MySql Slide 6
YOGESH SINGH
 
SQL for pattern matching (Oracle 12c)
Logan Palanisamy
 
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
11. using regular expressions with oracle database
Amrit Kaur
 
Advance sql session - strings
Eyal Trabelsi
 
Beg sql
KPNR Jan
 
regex_presentation.pptx
BeBetter4
 
Intro to T-SQL – 2nd session
Medhat Dawoud
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
libinjection: new technique in detecting SQLi attacks, iSEC Partners Open Forum
Nick Galbreath
 
vnd.openxmlformats-officedocument.presentationml.presentation&rendition=1.pptx
jaijsr12345
 
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
New features in abap
Srihari J
 
A SAS<sup>®</sup> Users Guide to Regular Expressions When the Data Resi...
Ken Borowiak
 
DP080_Lecture_2 SQL related document.pdf
MinhTran394436
 
Assg2 b 19121033-converted
SUSHANTPHALKE2
 
Structure Query Language (SQL).pptx
NalinaKumari2
 
Ad

Recently uploaded (20)

PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Ad

RPG Sql regular expression

  • 1. SQL Regular Expression A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is ^.*.txt$. (Definition from http://www.regular-expressions.info/).
  • 2. SQL Regular Expression There are many ways to use this as some example show on the internet, however I will use an example which I was challenged with to ilustrate how interesting it could be to use Regular Expression in SQL. I was asked to get all records from a table which C(50) field contain the below pattern anywhere in the field. Pattern 1: I###### 'I [0-9]{5}‘ character ‘I’ follow by 6 digits Pattern 2: I ##### 'I [0-9]{5}‘ character ‘I’ follow by space and then by 6 digits Pattern 3: IMP##### IMP [0-9]{5}‘ character IMP follow by 6 digits. Pattern 4: IMP ##### ‘I-[0-9]{5}‘ character IMP follow by space and then by 6 digits. Pattern 5: ###-#######-# [0-9]{3}-[0-9]{7}-[0-9]{1,} 3 digits, then dash, then 7 digits, then 1 digit and exact just one digits at the end, no more than one.
  • 3. SQL Regular Expression • 1). Select • 2). PLDESC, • 3). IFNULL(REGEXP_SUBSTR(PLDESC,'I [0-9]{5}'),'N'), • 4). IFNULL(REGEXP_SUBSTR(PLDESC,'IMP [0-9]{5}'),'N'), • 5). IFNULL(REGEXP_SUBSTR(PLDESC,'I-[0-9]{5}'),'N'), • 6). IFNULL(REGEXP_SUBSTR(PLDESC,'IMP[0-9]{5}'),'N'), • 7). IFNULL(REGEXP_SUBSTR(PLDESC,'I[0-9]{5}'),'N'), • 8). IFNULL(REGEXP_SUBSTR(PLDESC,'[0-9]{3}-[0-9]{7}-[0-9]{1,}'),'N') • 9). From apl • 10). Where • 11). REGEXP_like(PLDESC,'[0-9]{3}-[0-9]{7}-[0-9]{1}[^0-9]') Or • 12). REGEXP_LIKE(PLDESC,'I [0-9]{5}') Or • 13). REGEXP_LIKE(PLDESC,'IMP [0-9]{5}') Or • 14). REGEXP_LIKE(PLDESC,'I-[0-9]{5}') Or • 15). REGEXP_LIKE(PLDESC,'IMP[0-9]{5}') Or • 16). REGEXP_LIKE(PLDESC,'I[0-9]{5}'
  • 4. SQL Regular Expression 1). Sql select statement 2). Select Field PLDESC 3). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern I 5 digits(I #####) (I [0-9]{5}') if null, means the string does not have the pattern then instead of showing null (‘-’) show ‘N’ 4). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern IMP 5 digits(IMP #####)( Character IMP, follow by space and then follow by 5 digits) 5). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern I-5 digits(I--#####)( Character I-, follow by 5 digits) 'I-[0-9]{5}' if null means the string does not have the pattern then instead of Showing null (‘-’) show ‘N’. 6). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern IMP 5 digits(IMP #####)( Character IMP, follow by space and then follow by 5 digits) IMP[0-9]{5} if null means the string does not have the pattern then instead of Showing null (‘-’) show ‘N’. 7). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern I5 digits(I#####)( Character I, follow by 5 digits) 'IMP[0-9]{5}' if null means the string does not have the pattern then instead of Showing null (‘-’) show ‘N’
  • 5. SQL Regular Expression 8). Extract from the string field(REGEXP_SUBSTR) PLDESC the pattern ###-#######-# (3#-, dashes, 7#-, dashes, 1 or more #)( 3 digits follow by dashes, 7digits follow by dashes and then 1 or more digits, Digits at the end no character) ‘[ 0-9]{3}-[0-9]{7}-[0-9]{1,}' if null means the string does not have the pattern then instead of showing null (‘-’) show ‘N’. 9). From table APL 10). Where Clause 11). String contain the pattern'[0-9]{3}-[0-9]{7}-[0-9]{1}[^0-9]' (###-#######-#) (3 numbers, dashes, 7 numbers, dashes and one digit at the end no more than1). Pay Attention to this part [0-9]{1}[^0-9] that’s means 1 digit ([0-9]{1}) and after this no more digits ([^0-9]) Or. 12). String contain the pattern ‘I [0-9]{5}' which means Character ‘I’ follow by space and then 5 digits Or 13). String contain the pattern 'IMP [0-9]{5}' which means Character ‘IMP’ follow by space and then 5 digits Or. 14). String contain the pattern ‘I-[0-9]{5}’ which means Character ‘I’ follow by dash and then 5 digits Or. 15). String contain the pattern 'IMP[0-9]{5}' which means Character ‘IMP’ follow by 5 digits Or. 16). String contain the pattern 'I[0-9]{5}’’ which means Character ‘I’ follow by 5 digits. Note: this substring will be extracted it doesn’t matter in what position it exists in the string field PLDESC which is C(50). Will be extracted From anywhere it exists in the string field. There are other SQL REGEXP that maybe helpful in case you need it like REGEXP_REPLACE (Replace a pattern of string) REGEXP_INSTR (Give the position where a pattern of a string start) REGEXP_MATCH_COUNT (Count occurrences of a pattern in a string)