SlideShare ist ein Scribd-Unternehmen logo
1 von 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)

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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...
 

Ähnlich wie Extract patterns from SQL field using REGEXP

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
 

Ähnlich wie Extract patterns from SQL field using REGEXP (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
 

Kürzlich hochgeladen

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Kürzlich hochgeladen (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

Extract patterns from SQL field using REGEXP

  • 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)