LIKE in SQL

Facebooktwittergoogle_plusredditpinterestlinkedinmail

The LIKE operator in SQL Server allows you to query data using patterns instead of exact matches. Normal select statements using the = operator will only return records where there is an exact match (usually the casing or trailing spaces do not matter). With the LIKE operator, it does not need to be an exact match.

LIKE
The most basic concept of the like operator is that it allows you to query data without using an exact match in the WHERE clause. It allows you to use some wildcard characters to get the results you are looking for. Here is a basic example:

AnimalID AnimalName AnimalType Animal Weight
35 Giant Squid Fish 400
36 Giant Panda Mammal 500
37 Giant Clam Fish 60

The above statement uses the % as a wildcard. It basically says, show me any animals that start with the word Giant. This would return things like Giant Panda, Giant Squid, and Giant Clam.

Although % is the most common wildcard that is used with LIKE, there are many more. Here is a list of all the allowed wildcards.

%
Will allow zero or any characters. You can use this before, after, or in-between any string.

AnimalID AnimalName AnimalType Animal Weight
35 Giant Squid Fish 400
36 Giant Panda Mammal 500
37 Giant Clam Fish 60
38 Emperor Penguin Bird 15
39 Hammer Head Shark Fish 90

 
_
Will allow any 1 character.

AnimalID AnimalName AnimalType Animal Weight
18 Chicken Bird 5

 
[]
Will allow 1 character that is specified in the brackets. There are two ways to specify this. [a-d] or [abcd].

The following example will match both goose and moose.

AnimalID AnimalName AnimalType Animal Weight
11 Goose Bird 15
27 Moose Mammal 1000

 
The following example will match both cat and bat, but it will NOT match rat.

AnimalID AnimalName AnimalType Animal Weight
10 Cat Mammal 10
34 Bat Bird 1

 
[^]
Will match any 1 character that is NOT specified in the brackets. There are two ways to specify this. [^a-d] or [^abcd].

The following example will match horse, but will NOT match zorse… and yes, zorse is an animal… I found it on the internet.

AnimalID AnimalName AnimalType Animal Weight
6 Horse Mammal 750

 
The following example will match rat, but will NOT match bat or cat.

AnimalID AnimalName AnimalType Animal Weight
33 Rat Mammal 1

 
Important
If you are comparing a CHAR data type with the like operator, it may not work correctly. This because when you save data to a CHAR field it will space pad the field to the size of the field. To get around this, you need to trim the spaces from the end of the field.

 
NOT Like
If you want to match where a pattern is NOT like a string, simply put the word NOT in front of the word LIKE.

 
 
Escaping The Wildcard
From time to time you will need to actually search for a pattern containing one of the wildcard characters. Let’s say that you wanted to search for anything in a string that had 10% in it. The % (percent symbol) is a reserved word with the LIKE operator. You would search for the % character by using the ESCAPE clause.

In the query above, you can see that !% in the comparison string means to the literal character %. The ESCAPE clause tells the query to not apply the wildcard rules to any character following the specified characters. In this case, we are specifying the ! (exclamation mark).

InvoiceID ItemName LineItemDescription
1 Coupon Code 10% off web coupon

 
Reference: https://msdn.microsoft.com/en-us/library/ms179859.aspx

Leave a Comment

CommentLuv badge