SQL โ€“ Like

SQL Like Clause

Like operator is used to filter the results . Like operator compares the results using the wild cards.

There are two types of wildcards used for the filtering & matching the result.

1. % โ€“ (Percent Symbol).
2. _ โ€“ (Underscore Symbol).

SQL Like CLause Syntax (Wildcard %)

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜%Patternโ€™;

Or

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜Pattern%โ€™;

or

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜%Pattern%โ€™;

Explanation :

โ€˜Pattern%โ€™ โ€“ : Mathes & finds the pattern where column value starts with the provided pattern (Ex . โ€“ โ€˜%Johnโ€™ โ€“ Will find the records where Name(Column) starts with โ€˜Johnโ€™).

โ€˜%Patternโ€™ โ€“ : Mathes & finds the pattern where column value ends with the provided pattern (Ex . โ€“ โ€˜John%โ€™ โ€“ Will find the records where Name(Column) ends with โ€˜Johnโ€™ ).

โ€˜%Pattern%โ€™ โ€“ : Mathes & finds the pattern where pattern find at any place in column value (Ex . โ€“ โ€˜%John%โ€™ โ€“ Will find the records where Name(Column) find at any where ).

SQL Like CLause Syntax (Wildcard _ ) โ€“

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜_Patternโ€™;

Or

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜Pattern_โ€™;

or

SELECT columnName1, ColumnName2.. FROM TableName WHERE ColumnName LIKE โ€˜_Pattern_โ€™;

Explanation :

โ€˜_Patternโ€™ โ€“ : Mathes & finds the pattern where column value has the pattern at second place (Ex . โ€“ โ€˜_Jโ€™ โ€“ Will match the column value where second value is โ€˜Jโ€™).

โ€˜Pattern_โ€™ โ€“ : Mathes & finds the pattern where column value start with the pattern (Ex . โ€“ โ€˜J%โ€™ โ€“ Will match the column value where column value starts with โ€˜Jโ€™ ).

โ€˜_Pattern_โ€™ โ€“ : Mathes & finds the pattern where pattern where (Ex . โ€“ โ€˜_Jn_โ€™ โ€“ Will find the records where 2nd value is โ€˜Jโ€™ and seond last value is โ€˜nโ€™ ).

Example :

Example

SELECT *FROM Users WHERE Name LIKE โ€˜J%โ€™;

Will Produce The Follwoing Result.

$like

Example :

Example

SELECT *FROM Users WHERE Name LIKE โ€˜%iโ€™;

Will Produce The Follwoing Result.

SQL Like


Advertisements

Add Comment

๐Ÿ“– Read More