Arrow

SQL like operator

AuthorHariom Prajapati

Pubish Date03 Jul 2022

categoryMySql

Sql like operator use with where clause to search any specific data from a column.

Suppose we need to find any data from a column but we only know the first one or two character then we need to use like operator.

  • The percent sign ( % ) used for zero, one or more characters.
  • The Underscore sign ( _ ) use for one or single character.

 

Syntax - 

select column_name from table_name  where column_name like pattern;

 

Some example for different like operators with percent ( % ) and underscore ( _ ).

LIKE Operator

Description

WHERE CustomerName LIKE 'a%'

Finds values which start with "a"

WHERE CustomerName LIKE '%a'

Finds values which end with "a"

WHERE CustomerName LIKE '%or%'

Finds values which have "or" in any position

WHERE CustomerName LIKE '_r%'

Finds values which have "r" in the second position

WHERE CustomerName LIKE 'a_%'

Finds values which start with "a" and are at least 2 characters in length

WHERE CustomerName LIKE 'a__%'

Finds values which start with "a" and are at least 3 characters in length

WHERE ContactName LIKE 'a%o'

Finds values which start with "a" and ends with "o"