-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREGEX_9.sql
60 lines (46 loc) · 1.48 KB
/
REGEX_9.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
USE sql_store;
-- REGULAR EXPRESSSIONS can be used for pattern matching just like wildcards but are more powerful as seen below.
-- Can use % for the following pattern matching.
SELECT *
FROM customers
WHERE last_name LIKE "%field%";
-- REGEXP can be used to rewrite the same query.
SELECT *
FROM customers
WHERE last_name REGEXP "field"; -- checks for any occurence of "field" in the value
-- Can use ^ to denote the start of the value.
SELECT *
FROM customers
WHERE last_name REGEXP "^R"; -- requires "field" to be the start of the value.
SELECT *
FROM Patients P
WHERE P.conditions REGEXP "^DIAB1| DIAB1"; -- Notice the whitespace before DIAB1 after |
-- Can use a $ to indicate the end of the value.
SELECT *
FROM customers
WHERE last_name REGEXP "t$" -- requires "field" to be the start of the value.
-- Can use a | to find multiple patterns at once.
SELECT *
FROM customers
WHERE last_name REGEXP "mac|field|rose" ;
SELECT *
FROM customers
WHERE last_name REGEXP "mac$|field$|^rose" ;
-- Matching any of a multiple of values
SELECT *
FROM customers
WHERE last_name REGEXP "[gar]e"; -- matches any "ge" or "ae" or "re"
SELECT *
FROM customers
WHERE last_name REGEXP "e[gty]"; -- matches any "eg" or "et" or "ey"
-- Can even supply a range of character.
SELECT *
FROM customers
WHERE last_name REGEXP "e[a-f]";
-- Get the customers with the last names that staer with MY or end with SE
SELECT *
FROM customers
WHERE last_name REGEXP "^MY|SE$";
SELECT *
FROM customers
WHERE last_name REGEXP "B[RU]";