Regular Expression को हम RegEx फंक्शन भी कहते है इसका उपयोग characters के विभिन्न sequences को patterns से match करने के लिए किया जाता है।
RegEx फंक्शन बहुत सारे अलग अलग data type integer, special characters, Strings, आदि का combination हो सकता है। डेटाबेस से डाटा के एक छोटे subset को खोजने के लिए हम Regex फंक्शन का उपयोग करते है।
Syntax for using SQL Regex
SELECT statements… WHERE field_name REGEXP 'my_pattern';
Example : Student Table
Student_ID | Name | Email_id | Course_id | Course_name |
1001 | Amit | Amit2525@gmail.com | 1234 | Python |
1002 | Sachin | Sachin2867@gmail.com | 1235 | C++ |
1003 | Divya | Div395@yahoo.com | 1234 | Python |
1004 | Shivani | shivani9685@gmail.com | 1238 | Sql |
1005 | Anup | Anup1265@yahoo.com | 1238 | Sql |
1006 | Abhishek | Abhi4265@hotmail.com | 1236 | java |
Query1 : Student table से किसी specified String को match करना।
select * from student where course_name regexp 'python';
OUTPUT
1001 Amit Amit2525@gmail.com 1234 Python
1003 Divya Div395@yahoo.com 1234 Python
Query 2 : Student table से ऐसे String को match करना। जिसकी ending @hotmail.com से हो। इसके लिए हम $ pattern का उपयोग करेंगे।
select * from student where email_id regexp '@hotmail.com$' ;
OUTPUT
1006 Abhishek Abhi4265@hotmail.com 1236 java
Query 3 : Student table के name कॉलम से ऐसे String को match करना। जिसमे कोई नाम Di से शुरू हो।
select * from student where name regexp 'di?';
OUTPUT
1003 Divya Div395@yahoo.com 1234 Python