-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regular Expressions in SQL.sql
79 lines (61 loc) · 1.92 KB
/
Regular Expressions in SQL.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
SELECT *
FROM Course
WHERE CourseID LIKE '_____';
SELECT *
FROM Course
WHERE REGEXP_LIKE(CourseID, '\w\w\w\w\w');
SELECT *
FROM Course
WHERE REGEXP_LIKE(CourseID, '\w\w\w\w\w\w');
SELECT *
FROM Course
WHERE REGEXP_LIKE(CourseID, '\w{6}');
SELECT *
FROM Course
WHERE REGEXP_LIKE(Credits, '\d');
SELECT *
FROM Course
WHERE REGEXP_LIKE(CourseID, 'CSC\w\w\w');
SELECT *
FROM Course
WHERE REGEXP_LIKE(CourseID, 'CSC\w\w2');
SELECT *
FROM Course
WHERE REGEXP_LIKE(Name, 'a');
SELECT *
FROM Course
WHERE REGEXP_LIKE(Name, '^D');
DROP TABLE Contacts;
CREATE TABLE Contacts
(
L_Name VARCHAR2(30),
P_Number VARCHAR2(30),
CONSTRAINT Contacts_P_Number_CHECK
CHECK (REGEXP_LIKE(P_number, '^\(\d{3}\) \d{3}-\d{4}$'))
);
-- Success to insert
INSERT INTO Contacts (P_number) VALUES('(650) 555-5555');
INSERT INTO Contacts (P_number) VALUES('(215) 555-3427');
-- Fail to insert
INSERT INTO Contacts (P_number) VALUES(' (215) 555-3427');
INSERT INTO Contacts (P_number) VALUES('650 555-5555');
INSERT INTO Contacts2 (P_number) VALUES('(650) 555 5555');
INSERT INTO Contacts (P_number) VALUES('(650) 5555555');
INSERT INTO Contacts (P_number) VALUES('(650)555-5555');
DROP TABLE Contacts2;
CREATE TABLE Contacts2
(
L_Name VARCHAR2(30),
P_Number VARCHAR2(30),
CONSTRAINT Contacts2_P_Number_CHECK
CHECK (REGEXP_LIKE(P_number, '^\(?\d{3}\)? ?\d{3}-?\d{4}$'))
);
-- Success to insert
INSERT INTO Contacts2 (P_number) VALUES('(650) 555-5555');
INSERT INTO Contacts2 (P_number) VALUES('(215) 555-3427');
INSERT INTO Contacts2 (P_number) VALUES('650 555-5555');
INSERT INTO Contacts2 (P_number) VALUES('(650) 5555555');
INSERT INTO Contacts2 (P_number) VALUES('(650)555-5555');
-- Fail to insert
INSERT INTO Contacts2 (P_number) VALUES(' (215) 555-3427');
INSERT INTO Contacts2 (P_number) VALUES('(650) 555 5555');