Using SQL LIKE operator

No comments
SQL LIKE operator is used for pattern matching with WHERE clause. For example if we want to get all the records of customers with names starting with J, we can use this LIKE operator with where clause.
This LIKE operator is used with some WILDCARDS.

% - (Percentage sign)It is used to represent zero, one or multiple characters.

_ - (underscore) It is used to represent single character.

SQL LIKE operator syntax and example -

Suppose we want to get the rows with FNAME starting with 'J', we can use the following statement -
 SELECT * FROM CUSTOMER WHERE FNAME LIKE 'J%';
+----+--------+---------+------+-----------+
| ID | FNAME  | LNAME   | AGE  | COUNTRY   |
+----+--------+---------+------+-----------+
|  2 | JONTY  | RHODES  |   40 | CHINA     |
|  3 | JOHNY  | DEPP    |   45 | INDIA     |
|  4 | JESSIE | PINKMAN |   25 | AUSTRALIA |
|  5 | JESSIE | JACKSON |   22 | CANADA    |
+----+--------+---------+------+-----------+
4 rows in set (0.00 sec)
Similarly we use LIKE operator with these wildcards in different ways.

No comments :

Post a Comment