Select Query Using Like Clause in SQL Server 2012

The "like" operator is used in a "where" clause to search for a specified pattern of characters using the wildcard mechanism in a column. Wildcard characters make the "like" operator more flexible than using = and != (Not Equal To) string comparison operators. To search for a character string using one or more wildcard characters in a LIKE query, simply include the wildcards in a string literal along with the portion of the string. So let's take a look at a practical example of how to use a like operator to search in SQL Server. The example is developed in SQL Server 2012 using the SQL Server Management Studio.
 
Wildcard Characters
 
There are four types of wildcard characters in SQL Server:
  1. Percent sign (%)
  2. Underscore (_)
  3. Bracket ([])
  4. Caret (^)
Creating a Table in SQL Server
 
The table looks as in the following figure:
 
Creating a Table in SQL Server
 
Now we perform searching on the above table using wildcard characters.
 
Like with Percent sign (%)
 
It is used to search any string of zero or more characters.
  1. SELECT * FROM UserDetail WHERE FirstName LIKE 'ra%' -- return all columns from UserDetail table where FirstName name start with 'ra'    
  2.     
  3. SELECT * FROM UserDetail WHERE FirstName LIKE '%n'   -- return all columns from UserDetail table where FirstName name End with 'n'    
  4.     
  5. SELECT * FROM UserDetail WHERE FirstName LIKE '%an%'-- return all columns from UserDetail table where FirstName name have the letters End with 'an'  
The result table will show the following information:
 
Percent wildcard character in SQL Server
 
Like with Underscore (_)
 
It is used to search for a single character.
  1. SELECT * FROM UserDetail WHERE FirstName LIKE 'rah_' 
  2. - return all columns from UserDetail table where FirstName name is Four characters long and start with 'rah'  
  3.   
  4. SELECT * FROM UserDetail WHERE FirstName LIKE '_ahu' 
  5. - return all columns from UserDetail table where FirstName name is Four characters long End with 'ahu'  
  6.   
  7. SELECT * FROM UserDetail WHERE FirstName LIKE '__e' 
  8. - return all columns from UserDetail table where FirstName name start and end with any character whereas the middle character must be 'e'. 
The result table will show the following information:

Underscore wildcard character in SQL-Server
 
Like with Bracket ([])
 
It is used to search for any single character within the specified range.
  1. SELECT * FROM UserDetail WHERE FirstName LIKE '[rc]%' 
  2. -- return all columns from UserDetail table where FirstName name begin with 'R' or 'C'  
  3.   
  4. SELECT * FROMUserDetail WHERE FirstName LIKE '[rc]%u'  
  5. -- return all columns from UserDetail table where FirstName name begin with 'R' or 'C' and end with 'U'.  
  6.   
  7. SELECT * FROM UserDetail WHERE FirstName LIKE 'R[a]%'
  8. -- return all columns from UserDetail table where FirstName name begin with 'R' and hold an 'a' in the second place.  
The result table will show the following information:
 
Bracket wildcard Character in SQL Server
 
Like with Caret (^)
 
It is used to search for any single character that is not the specified character.
  1. SELECT * FROM UserDetail WHERE FirstName LIKE 'R[^H]%'   
  2. --return all columns from UserDetail table where FirstName name begin with 'R' and does not contain an 'H'  as the second place
  3.   
  4. SELECT * FROM UserDetail WHERE FirstName LIKE '%N[^R]'  
  5. -- return all columns from UserDetail table where FirstName name Contain with 'N' and does not contain an 'R' 
The result table will show the following information:
 
Caret wildcard character in SQL Server


Similar Articles