SlideShare a Scribd company logo
1 of 5
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

Bring sanity back to sql (advance sql)
Bring sanity back to sql (advance sql)Bring sanity back to sql (advance sql)
Bring sanity back to sql (advance sql)Eyal Trabelsi
 
Constructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languagesConstructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languagesOliverYoung22
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintfteach4uin
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88Mahmoud Samir Fayed
 
Arrays and Pointers
Arrays and PointersArrays and Pointers
Arrays and PointersSimoniShah6
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)Sanjay Saha
 
Introduction_modern_fortran_short
Introduction_modern_fortran_shortIntroduction_modern_fortran_short
Introduction_modern_fortran_shortNils van Velzen
 

What's hot (12)

Bring sanity back to sql (advance sql)
Bring sanity back to sql (advance sql)Bring sanity back to sql (advance sql)
Bring sanity back to sql (advance sql)
 
Constructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languagesConstructs and techniques and their implementation in different languages
Constructs and techniques and their implementation in different languages
 
T03 a basicioprintf
T03 a basicioprintfT03 a basicioprintf
T03 a basicioprintf
 
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.3 book - Part 11 of 88
 
Arrays and Pointers
Arrays and PointersArrays and Pointers
Arrays and Pointers
 
Stack and Queue (brief)
Stack and Queue (brief)Stack and Queue (brief)
Stack and Queue (brief)
 
Introduction_modern_fortran_short
Introduction_modern_fortran_shortIntroduction_modern_fortran_short
Introduction_modern_fortran_short
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
1. 5 Circular singly linked list
1. 5 Circular singly linked list1. 5 Circular singly linked list
1. 5 Circular singly linked list
 
Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...
Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...
Algorithm Class is a Training Institute on C, C++,C#, CPP, DS, JAVA, data str...
 
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
 
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
Algorithm Class is a Training Institute on C, C++, CPP, DS, JAVA, data struct...
 

Similar to RPG Sql regular expression

regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdfDarellMuchoko
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)Logan Palanisamy
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6YOGESH SINGH
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptxMrhaider4
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Wilson Su
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
regex_presentation.pptx
regex_presentation.pptxregex_presentation.pptx
regex_presentation.pptxBeBetter4
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript ObjectsReem Alattas
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 

Similar to RPG Sql regular expression (20)

regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Ch2
Ch2Ch2
Ch2
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
Array and string
Array and stringArray and string
Array and string
 
Array assignment
Array assignmentArray assignment
Array assignment
 
regex_presentation.pptx
regex_presentation.pptxregex_presentation.pptx
regex_presentation.pptx
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
Functions
FunctionsFunctions
Functions
 
Ch08
Ch08Ch08
Ch08
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 

Recently uploaded (20)

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 

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)