<-- Chapter 22: SQL Select Top
Chapter 23
SQL Wildcards
SQL Wildcard characters are used to substitute any characters in a string. As we have discussed LIKE operator in chapter 10. SQL supports two wildcard operators with LIKE operator i.e (%) and (_) underscore sign. The wildcards used in SQL are : -
-
% : Zero or more characters can be substitute.
-
_ : One character can be substitute.
-
[charlist] : It sets and ranges of characters which are specified within the brackets to match in the table records.
-
[!charlist] or [^charlist] : It matches only the characters which are not specified within the brackets to match in the table records.
Lets see an examples from the below table "employees" :-
employee_id |
name |
code |
designation |
salary |
101 |
ABC |
E-101 |
Engineer |
12000 |
102 |
DEF |
E-102 |
Doctor |
8000 |
103 |
GHI |
E-103 |
Software Developer |
8000 |
104 |
JKL |
E-104 |
CEO |
12000 |
105 |
MNO |
E-105 |
Software Developer |
100000 |
% Wildcard SQL syntax :-
SELECT *
FROM `employees`
WHERE `designation` LIKE 'sof%'
Now run the above SQL query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
103 |
GHI |
E-103 |
Software Developer |
8000 |
105 |
MNO |
E-105 |
Software Developer |
100000 |
Now we can see here, those records are displayed which column designation values matched with the starting letters 'sof'.
_ Wildcard SQL syntax :-
SELECT * FROM `employees`
WHERE designation LIKE '_octor';
Now run the above SQL query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
102 |
DEF |
E-102 |
Doctor |
8000 |
Now we can see here, only those Records are displayed which column designation value starting with any character, followed by "octor".
[charlist] Wildcard SQL syntax :-
SELECT * FROM `employees`
WHERE `designation` LIKE '[edc]%';
Now run the above SQL query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
101 |
ABC |
E-101 |
Engineer |
12000 |
102 |
DEF |
E-102 |
Doctor |
8000 |
104 |
JKL |
E-104 |
CEO |
12000 |
Now we can see here, only those Records are displayed which column designation value starting with "e", "d", or "c":
[!charlist] Wildcard SQL syntax :-
SELECT * FROM `employees`
WHERE `designation` LIKE '[!edc]%';
Now run the above SQL query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
103 |
GHI |
E-103 |
Software Developer |
8000 |
105 |
MNO |
E-105 |
Software Developer |
100000 |
Now we can see here, only those Records are displayed which column designation value does not starts with "e", "d", or "c":
Chapter 24: SQL Aliases -->
0 Comment(s)