Friday, June 19, 2015

LIKE keyword

LIKE Keyword

When retrieving data, you can view selected rows that match a specific pattern. For example, you are asked to create a report that displays the names of all the products of AdventureWorks beginning with the letter P. You can do this by using the LIKE keyword.
The LIKE keyword is used to search a string by using wildcards. Wildcards are special characters, such as ‘*’ and ‘%’. These characters are used to match patterns. The LIKE keyword matches the given character string with the specified pattern. The pattern can include combination of wildcard characters and regular characters. While performing a pattern match, regular characters must match the characters specified in the character string.

SELECT * FROM HumanResources.Department WHERE Name LIKE 'Pro%' 

The following table describes the use of the wildcard characters with the LIKE keyword.
Expression
 Returns LIKE ‘LO%’ All names that begin with “LO”
 LIKE ‘%ion’ All names that end with “ion” LIKE ‘%rt%’ All names that have the letters “rt” in them
 LIKE ‘_rt’ All three letter names ending with “rt”
 LIKE ‘[DK]%’ All names that begin with “D” or “K” 
LIKE ‘[A D]ear’ All four letter names that end with “ear” and begin with any letter from “A” through “D” 
LIKE ‘D[^c]%’ All names beginning with “D” and not having “c” as the second letter.

The Like operator is not case- sensitive. For example, Like 'LO%' and Like 'lo%' will return the same result.

Tuesday, June 16, 2015