LIKE (Transact-SQL) - SQL Server - Microsoft Learn
LIKE (Transact-SQL) - SQL Server - Microsoft Learn
Article • 03/13/2023
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure
Synapse Analytics Analytics Platform System (PDW)
Determines whether a specific character string matches a specified pattern. A pattern can include
regular characters and wildcard characters. During pattern matching, regular characters must
exactly match the characters specified in the character string. However, wildcard characters can be
matched with arbitrary fragments of the character string. Using wildcard characters makes the
LIKE operator more flexible than using the = and != string comparison operators. If any one of
the arguments isn't of character string data type, the SQL Server Database Engine converts it to
character string data type, if it's possible.
Syntax
Syntax for SQL Server and Azure SQL Database:
syntaxsql
syntaxsql
ESCAPE and STRING_ESCAPE are not supported in Azure Synapse Analytics or Analytics Platform
System (PDW).
7 Note
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions
documentation.
Arguments
match_expression
Any valid expression of character data type.
pattern
The specific string of characters to search for in match_expression, and can include valid wildcard
characters in the following table. pattern can be a maximum of 8,000 bytes.
If match_expression is a higher precedence data type than pattern, and the pattern length is greater
than match_expression, you will get a truncation error during the implicit conversion of pattern
value to match_expression type.
% Any string of zero or WHERE title LIKE '%computer%' finds all book titles with the word
more characters. computer anywhere in the book title.
_ Any single character. WHERE au_fname LIKE '_ean' finds all four-letter first names that
(underscore) end with ean ( Dean , Sean , and so on).
[ ] Any single character WHERE au_lname LIKE '[C-P]arsen' finds author last names ending
within the specified with arsen and starting with any single character between C and
range [a-f] or set P , for example Carsen , Larsen , Karsen , and so on. In range
[abcdef] . searches, the characters included in the range may vary
depending on the sorting rules of the collation.
[^] Any single character not WHERE au_lname LIKE 'de[^l]%' finds all author last names starting
within the specified with de and where the following letter isn't l .
range [^a-f] or set
[^abcdef] .
escape_character
A character put in front of a wildcard character to indicate that the wildcard is interpreted as a
regular character and not as a wildcard. escape_character is a character expression that has no
default and must evaluate to only one character.
Result type
Boolean
Result value
LIKE returns TRUE if the match_expression matches the specified pattern.
Remarks
When you do string comparisons by using LIKE , all characters in the pattern string are significant.
Significant characters include any leading or trailing spaces. If a comparison in a query is to return
all rows with a string LIKE 'abc ' ( abc followed by a single space), a row in which the value of
that column is abc ( abc without a space) isn't returned. However, trailing blanks, in the expression
to which the pattern is matched, are ignored. If a comparison in a query is to return all rows with
the string LIKE 'abc' ( abc without a space), all rows that start with abc and have zero or more
trailing blanks are returned.
A string comparison using a pattern that contains char and varchar data may not pass a LIKE
comparison because of how the data is stored for each data type. The following example passes a
local char variable to a stored procedure, and then uses pattern matching to find all employees
whose last names start with the specified set of characters.
SQL
-- Uses AdventureWorks
SELECT p.FirstName,
p.LastName,
a.City
FROM Person.Person p
INNER JOIN Person.Address a
ON p.BusinessEntityID = a.AddressID
WHERE p.LastName LIKE @EmpLName;
GO
In the FindEmployee procedure, no rows are returned because the char variable ( @EmpLName )
contains trailing blanks whenever the name contains fewer than 20 characters. Because the
LastName column is varchar, there are no trailing blanks. This procedure fails because the trailing
blanks are significant.
However, the following example succeeds because trailing blanks aren't added to a varchar
variable.
SQL
-- Uses AdventureWorks
SELECT p.FirstName,
p.LastName,
a.City
FROM Person.Person p
INNER JOIN Person.Address a
ON p.BusinessEntityID = a.AddressID
WHERE p.LastName LIKE @EmpLName;
GO
Output
(match_expression, pattern, and escape_character, if present) are ASCII character data types, ASCII
pattern matching is performed. If any one of the arguments are of Unicode data type, all
arguments are converted to Unicode, and Unicode pattern matching is performed. When you use
Unicode data (nchar or nvarchar data types) with LIKE , trailing blanks are significant; however, for
non-Unicode data, trailing blanks aren't significant. Unicode LIKE is compatible with the ISO
standard. ASCII LIKE is compatible with earlier versions of SQL Server.
The following series of examples shows the differences in rows returned between ASCII and
Unicode LIKE pattern matching.
SQL
INSERT INTO t
VALUES ('Robert King');
SELECT * FROM t
WHERE col1 LIKE '% King'; -- returns 1 row
INSERT INTO t
VALUES ('Robert King');
SELECT * FROM t
WHERE col1 LIKE '% King'; -- no rows returned
INSERT INTO t
VALUES ('Robert King');
SELECT * FROM t
WHERE RTRIM(col1) LIKE '% King'; -- returns 1 row
7 Note
LIKE comparisons are affected by collation. For more information, see COLLATE (Transact-
SQL).
For example, the following query shows all dynamic management views in the AdventureWorks2019
database, because they all start with the letters dm .
SQL
-- Uses AdventureWorks
SELECT Name
FROM sys.system_views
WHERE Name LIKE 'dm%';
GO
To see all objects that aren't dynamic management views, use NOT LIKE 'dm%' . If you have a total
of 32 objects and LIKE finds 13 names that match the pattern, NOT LIKE finds the 19 objects that
don't match the LIKE pattern.
You may not always find the same names with a pattern such as LIKE '[^d][^m]%' . Instead of 19
names, you may find only 14, with all the names that start with d or have m as the second letter
eliminated from the results, and the dynamic management view names. This behavior is because
match strings with negative wildcard characters are evaluated in steps, one wildcard at a time. If
the match fails at any point in the evaluation, it's eliminated.
Symbol Meaning
LIKE '5[%]' 5%
LIKE '[_]n' _n
LIKE '[a-cdf]' a , b , c , d , or f
LIKE '[-acdf]' - , a , c , d , or f
LIKE ']' ]
If there is no character after an escape character in the LIKE pattern, the pattern isn't valid and the
LIKE returns FALSE. If the character after an escape character isn't a wildcard character, the escape
character is discarded and the following character is treated as a regular character in the pattern.
These characters include the percent sign (%), underscore (_), and left bracket ([) wildcard
characters when they are enclosed in double brackets ([ ]). Escape characters can be used within
the double bracket characters ([ ]), including to escape a caret (^), hyphen (-), or right bracket (]).
0x0000 (char(0)) is an undefined character in Windows collations, and can't be included in LIKE.
Examples
SQL
-- Uses AdventureWorks
SELECT p.FirstName,
p.LastName,
ph.PhoneNumber
FROM Person.PersonPhone AS ph
INNER JOIN Person.Person AS p
ON ph.BusinessEntityID = p.BusinessEntityID
WHERE ph.PhoneNumber LIKE '415%'
ORDER BY p.LastName;
GO
Output
SQL
-- Uses AdventureWorks
SELECT p.FirstName,
p.LastName,
ph.PhoneNumber
FROM Person.PersonPhone AS ph
INNER JOIN Person.Person AS p
ON ph.BusinessEntityID = p.BusinessEntityID
WHERE ph.PhoneNumber NOT LIKE '415%'
AND p.FirstName = 'Gail'
ORDER BY p.LastName;
GO
Output
SQL
USE tempdb;
GO
IF EXISTS (
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'mytbl2'
)
DROP TABLE mytbl2;
GO
USE tempdb;
GO
INSERT mytbl2
VALUES ('Discount is 10-15% off'),
('Discount is .10-.15 off');
GO
SELECT c1
FROM mytbl2
WHERE c1 LIKE '%10-15!% off%' ESCAPE '!';
GO
SQL
-- Uses AdventureWorks
SELECT BusinessEntityID,
FirstName,
LastName
FROM Person.Person
WHERE FirstName LIKE '[CS]heryl';
GO
The following example finds the rows for employees in the Person table with last names of Zheng
or Zhang .
SQL
-- Uses AdventureWorks
SELECT LastName,
FirstName
FROM Person.Person
WHERE LastName LIKE 'Zh[ae]ng'
ORDER BY LastName ASC,
FirstName ASC;
GO
Examples: Azure Synapse Analytics and Analytics
Platform System (PDW)
SQL
-- Uses AdventureWorks
SELECT FirstName,
LastName,
Phone
FROM DimEmployee
WHERE phone LIKE '612%'
ORDER BY LastName;
SQL
-- Uses AdventureWorks
SELECT FirstName,
LastName,
Phone
FROM DimEmployee
WHERE phone NOT LIKE '612%'
ORDER BY LastName;
SQL
-- Uses AdventureWorks
SELECT FirstName,
LastName,
Phone
FROM DimEmployee
WHERE phone LIKE '6_2%'
ORDER BY LastName;
See also
PATINDEX (Transact-SQL)
Expressions (Transact-SQL)
What are the SQL database functions?
SELECT (Transact-SQL)
SELECT (Transact-SQL)
WHERE (Transact-SQL)