Thứ Hai, 24 tháng 7, 2023

Kiểm tra điều kiện với REGEXP_LIKE trong Oracle

Trong bài này mình sẽ hướng dẫn bạn sử dụng một lệnh kiểm tra điều kiện mới đó là REGEX_LIKE, lệnh này có công dụng giống như lệnh LIKE, chỉ có điều là lệnh này sử dụng để so khớp vỡi những khuôn mẫu phức tạp hơn, vì vậy bạn đừng hiểu lầm giữa REGEX_LIKE và LIKE nhé.

Mục lục

  • 1. Lệnh REGEXP_LIKE trong Oracle
    • Bảng regular expression
    • Bảng match_parameter
  • 2. Một vài ví dụ với REGEX_LIKE trong Oracle
  • 3. Lời kết

1. Lệnh REGEXP_LIKE trong Oracle

Bạn có thể sử dụng biểu thức chính quy Regular Expression để tạo khuôn mẫu, đây là một tính năng rất hay trong các hệ quản trị CSDL hiện nay như MySQL, SQL Server, Oracle ...

Cú pháp

1
REGEXP_LIKE ( expression, pattern [, match_parameter ] )

Trong đó:

  • expression là column bạn muốn kiểm tra
  • pattern là chuỗi pattern dùng để so khớp, sử dụng cú pháp regular expression (xem bảng phía dưới)
  • match_parameter có thể có hoặc không, nó cho phép phạm vi hoạt động của chuỗi regex (xem bảng phía dưới)

Bảng regular expression

ValueDescription
^Matches the beginning of a string. If used with a match_parameter of 'm', it matches the start of a line anywhere within expression.
$Matches the end of a string. If used with a match_parameter of 'm', it matches the end of a line anywhere within expression.
*Matches zero or more occurrences.
+Matches one or more occurrences.
?Matches zero or one occurrence.
.Matches any character except NULL.
|Used like an "OR" to specify more than one alternative.
[ ]Used to specify a matching list where you are trying to match any one of the characters in the list.
[^ ]Used to specify a nonmatching list where you are trying to match any character except for the ones in the list.
( )Used to group expressions as a subexpression.
{m}Matches m times.
{m,}Matches at least m times.
{m,n}Matches at least m times, but no more than n times.
\nn is a number between 1 and 9. Matches the nth subexpression found within ( ) before encountering \n.
[..]Matches one collation element that can be more than one character.
[::]Matches character classes.
[==]Matches equivalence classes.
\dMatches a digit character.
\DMatches a nondigit character.
\wMatches a word character.
\WMatches a nonword character.
\sMatches a whitespace character.
\Smatches a non-whitespace character.
\AMatches the beginning of a string or matches at the end of a string before a newline character.
\ZMatches at the end of a string.
*?Matches the preceding pattern zero or more occurrences.
+?Matches the preceding pattern one or more occurrences.
??Matches the preceding pattern zero or one occurrence.
{n}?Matches the preceding pattern n times.
{n,}?Matches the preceding pattern at least n times.
{n,m}?Matches the preceding pattern at least n times, but not more than m times.

Bảng match_parameter

ValueDescription
'c'Perform case-sensitive matching.
'i'Perform case-insensitive matching.
'n'Allows the period character (.) to match the newline character. By default, the period is a wildcard.
'm'expression is assumed to have multiple lines, where ^ is the start of a line and $ is the end of a line, regardless of the position of those characters in expression. By default, expression is assumed to be a single line.
'x'Whitespace characters are ignored. By default, whitespace characters are matched like any other character.

Mình sẽ không dịch sang tiếng Việt, bởi có những từ dịch ra sẽ khó hiểu hơn là để nguyên vậy ;) Nếu bạn không dịch được thì hãy sử dụng google translate nhé.

2. Một vài ví dụ với REGEX_LIKE trong Oracle

Sau đây là một vài ví dụ đơn giản sử dụng các biểu thức chính quy để tạo ra chuỗi pattern.

Ví dụ 1: Lấy danh sách sinh viên có tên là Tường hoặc Cường. Với ví dụ này mình sẽ sử dụng ký hiệu hoặc |.

1
2
3
SELECT *
FROM students
WHERE REGEXP_LIKE (last_name, '^(C|T)ường');

Ví dụ 2: Lấy danh sách sinh viên có tên bắt đầu bằng chữ A

1
2
3
SELECT *
FROM students
WHERE REGEXP_LIKE (last_name, '^A(*)');

Trường hợp này bạn cũng có thể bỏ cặp dấu ().

Ví dụ 3: Lấy danh sách sinh viên có tên kết thúc là chữ ng.

1
2
3
SELECT *
FROM students
WHERE REGEXP_LIKE (last_name, '(*)ng$');

3. Lời kết

Trên chỉ là kiến thức căn bản, bạn còn phải nghiên cứu nhiều thì mới hiểu hết tất cả các ký hiệu của biểu thức chính quy.

Tóm lại để học được bài này thì bạn phải có một chút kiến thức về lập trình nói chung và regular expression nói riêng

=============================
* KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE trực tiếp từ tôi giúp bạn bước đầu trở thành những chuyên gia DBA, đủ kinh nghiệm đi thi chứng chỉ OA/OCP, đặc biệt là rất nhiều kinh nghiệm, bí kíp thực chiến trên các hệ thống Core tại VN chỉ sau 1 khoá học.
* CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
* Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master