Pages

Showing posts with label SQL LIKE. Show all posts
Showing posts with label SQL LIKE. Show all posts

How to use LIKE?

LIKE operator is used to search for patterns in string.
It is used with 2 wild-cards:
  • _ means just a character
  • % means any number of character (it also means empty word)
Example: Select all employee records where employee name start with 'a' character and rest can be any thing.

SELECT * FROM tableEmployees WHERE employeeName LIKE 'a%'


Select all employee records where employee name 1st character is 'a', 2nd character is any thing, 3rd character is 'b' and the rest is any character sequence.

SELECT * FROM tableEmployees WHERE employeeName LIKE 'a_b%'

If we need to find '_' or '%' in a string we must put escape character before them.

Example: Select all image records from images table where image name start with 'IMAGE_' and the rest is anything.


SELECT * FROM images WHERE employeePhoto LIKE 'IMAGEID\_%' ESCAPE '\'