|
| 1 | +// LIKE with % WildCard in SQL |
| 2 | +-> The Like operator is used to search for a specified pattern in a column. |
| 3 | +syntax: SELECT * FROM table_name WHERE column_name LIKE 'pattern'; |
| 4 | + |
| 5 | +// Wild cards |
| 6 | +-> Wild cards are used to search for data within a table. These characters are used with thr LIKE operator. |
| 7 | +% -> zero or more characters |
| 8 | +_ -> One single character |
| 9 | +[charlist] -> Sets and ranges of characters to match |
| 10 | +[^charlist] or [!charlist] -> Matches only a character NOT specified within the brackets |
| 11 | + |
| 12 | +1. % -> zero or more characters |
| 13 | +'hello%' - all starting with hello Ex: hello testing |
| 14 | +ex: SELECT * FROM student_reg WHERE name LIKE 'bi%'; |
| 15 | ++------+-------+---------+------+---------+ |
| 16 | +| s_id | name | address | dob | fees | |
| 17 | ++------+-------+---------+------+---------+ |
| 18 | +| 104 | BIMAl | KTM | NULL | 1001.23 | |
| 19 | ++------+-------+---------+------+---------+ |
| 20 | + |
| 21 | +'%testing' - All ending with testing Ex: hello testing |
| 22 | +ex:SELECT * FROM student_reg WHERE name LIKE '%i'; |
| 23 | ++------+------+---------+------------+----------+ |
| 24 | +| s_id | name | address | dob | fees | |
| 25 | ++------+------+---------+------------+----------+ |
| 26 | +| 103 | HARI | PKR | 2001-01-12 | 10000.52 | |
| 27 | ++------+------+---------+------------+----------+ |
| 28 | + |
| 29 | +'%in%' - all containing with in ex: hello testing |
| 30 | +SELECT * FROM student_reg WHERE name LIKE '%ma%'; |
| 31 | ++------+-------+---------+------------+-----------+ |
| 32 | +| s_id | name | address | dob | fees | |
| 33 | ++------+-------+---------+------------+-----------+ |
| 34 | +| 104 | BIMAl | KTM | NULL | 1001.23 | |
| 35 | +| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | |
| 36 | ++------+-------+---------+------------+-----------+ |
| 37 | + |
| 38 | +2. - one single character |
| 39 | +'testing_ -> '-starting with show then any character Ex testing |
| 40 | +Ex: SELECT * FROM student_reg WHERE s_id LIKE '10_'; |
| 41 | ++------+---------+---------+------------+-----------+ |
| 42 | +| s_id | name | address | dob | fees | |
| 43 | ++------+---------+---------+------------+-----------+ |
| 44 | +| 101 | RAM | KTM | 1998-12-15 | 1000.23 | |
| 45 | +| 102 | SITA | KTM | 2001-10-11 | 200000.12 | |
| 46 | +| 103 | HARI | PKR | 2001-01-12 | 10000.52 | |
| 47 | +| 104 | BIMAl | KTM | NULL | 1001.23 | |
| 48 | +| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | |
| 49 | +| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | |
| 50 | ++------+---------+---------+------------+-----------+ |
| 51 | +'_tin' -> ant charaxter the tin. Ex testing |
| 52 | +ex:SELECT * FROM student_reg WHERE s_id LIKE '_04'; |
| 53 | ++------+-------+---------+------+---------+ |
| 54 | +| s_id | name | address | dob | fees | |
| 55 | ++------+-------+---------+------+---------+ |
| 56 | +| 104 | BIMAl | KTM | NULL | 1001.23 | |
| 57 | ++------+-------+---------+------+---------+ |
| 58 | +'t_s_g -> t then any character then s then any character, then g ex: testing |
| 59 | +ex: SELECT * FROM student_reg WHERE s_id LIKE '1_3'; |
| 60 | ++------+------+---------+------------+----------+ |
| 61 | +| s_id | name | address | dob | fees | |
| 62 | ++------+------+---------+------------+----------+ |
| 63 | +| 103 | HARI | PKR | 2001-01-12 | 10000.52 | |
| 64 | ++------+------+---------+------------+----------+ |
0 commit comments