{"id":301,"date":"2018-04-29T11:23:40","date_gmt":"2018-04-29T04:23:40","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=301"},"modified":"2024-03-02T15:50:56","modified_gmt":"2024-03-02T08:50:56","slug":"sql-server-like","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/","title":{"rendered":"SQL Server LIKE Operator"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL Server <code>LIKE<\/code> operator to check whether a character string matches a specified pattern.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-server-like-operator'>Introduction to SQL Server LIKE operator <a href=\"#introduction-to-sql-server-like-operator\" class=\"anchor\" id=\"introduction-to-sql-server-like-operator\" title=\"Anchor for Introduction to SQL Server LIKE operator\">#<\/a><\/h2>\n\n\n\n<p>The SQL Server <code>LIKE<\/code> operator is a logical operator that checks if a character string matches a specified pattern. <\/p>\n\n\n\n<p>A pattern may include regular characters and wildcard characters. The <code>LIKE<\/code> operator is used in the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code> clause of the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">SELECT<\/a><\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-update\/\">UPDATE<\/a><\/code>, and <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-delete\/\">DELETE<\/a><\/code> statements\u00a0to filter rows based on pattern matching.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>LIKE<\/code> operator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column | expression LIKE pattern &#91;ESCAPE escape_character]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='pattern'>Pattern <a href=\"#pattern\" class=\"anchor\" id=\"pattern\" title=\"Anchor for Pattern\">#<\/a><\/h3>\n\n\n\n<p>The pattern is a sequence of characters to search for in the column or expression. It can include the following valid wildcard characters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The percent wildcard (%): any string of zero or more characters.<\/li>\n\n\n\n<li>The underscore (_) wildcard: any single character.<\/li>\n\n\n\n<li>The [list of characters] wildcard: any single character within the specified set.<\/li>\n\n\n\n<li>The [character-character]: any single character within the specified range.<\/li>\n\n\n\n<li>The [^]: any character that is not within a list or a range.<\/li>\n<\/ul>\n\n\n\n<p>The wildcard characters make the <code>LIKE<\/code> operator more flexible than the equal (=) and not equal (!=) string comparison operators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='escape-character'>Escape character <a href=\"#escape-character\" class=\"anchor\" id=\"escape-character\" title=\"Anchor for Escape character\">#<\/a><\/h3>\n\n\n\n<p>The escape character instructs the <code>LIKE<\/code> operator to treat the wildcard characters as regular characters. The escape character has no default value and must be evaluated to only one character.<\/p>\n\n\n\n<p>The <code>LIKE<\/code> operator returns <code>TRUE<\/code> if the column or expression matches the specified pattern.<\/p>\n\n\n\n<p>To negate the result of the <code>LIKE<\/code> operator, you use the <code>NOT<\/code> operator as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">column | expression NOT LIKE pattern &#91;ESCAPE escape_character]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-like-operator-examples'>SQL Server LIKE operator examples <a href=\"#sql-server-like-operator-examples\" class=\"anchor\" id=\"sql-server-like-operator-examples\" title=\"Anchor for SQL Server LIKE operator examples\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll use the following <code>customers<\/code> table from the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"181\" height=\"231\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png\" alt=\"customers table\" class=\"wp-image-158\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-like-operator-with-the-wildcard-examples'>1) Using the LIKE operator with the % wildcard examples <a href=\"#1-using-the-like-operator-with-the-wildcard-examples\" class=\"anchor\" id=\"1-using-the-like-operator-with-the-wildcard-examples\" title=\"Anchor for 1) Using the LIKE operator with the % wildcard examples\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>LIKE<\/code> operator with the <code>%<\/code> wildcard to find the customers whose last name starts with the letter <code>z<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'z%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"223\" height=\"73\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-example.png\" alt=\"SQL Server LIKE example\" class=\"wp-image-307\"\/><\/figure>\n\n\n\n<p>The following example uses the <code>LIKE<\/code> operator with the <code>%<\/code> wildcard to return the customers whose last name ends with the\u00a0string <code>er<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%er'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"206\" height=\"225\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-percent-example.png\" alt=\"SQL Server LIKE percent example\" class=\"wp-image-308\"\/><\/figure>\n\n\n\n<p>The following statement uses the <code>LIKE<\/code> operator to retrieve the customers whose last name starts with the letter <code>t<\/code> and ends with the letter <code>s<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'t%s'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=''><figure><img loading=\"lazy\" decoding=\"async\" class=\"border alignnone wp-image-309 size-full\" title=\"SQL Server LIKE percent wildcard example\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-percent-wildcard-example.png\" alt=\"SQL Server LIKE percent wildcard example\" width=\"202\" height=\"91\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-percent-wildcard-example.png 202w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-percent-wildcard-example-200x91.png 200w\" sizes=\"auto, (max-width: 202px) 100vw, 202px\" \/><\/figure> <a href=\"#\" class=\"anchor\" id=\"\" title=\"Anchor for &lt;figure&gt;&lt;img class=&quot;border alignnone wp-image-309 size-full&quot; title=&quot;SQL Server LIKE percent wildcard example&quot; src=&quot;https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-percent-wildcard-example.png&quot; alt=&quot;SQL Server LIKE percent wildcard example&quot; width=&quot;202&quot; height=&quot;91&quot;&gt;&lt;\/figure&gt;\">#<\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-like-operator-with-the-_-underscore-wildcard-example'>2) Using the LIKE operator with the _ (underscore) wildcard example <a href=\"#2-using-the-like-operator-with-the-_-underscore-wildcard-example\" class=\"anchor\" id=\"2-using-the-like-operator-with-the-_-underscore-wildcard-example\" title=\"Anchor for 2) Using the LIKE operator with the _ (underscore) wildcard example\">#<\/a><\/h3>\n\n\n\n<p>The underscore represents a single character. For example, the following statement returns the customers where the second character is the letter <code>u<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'_u%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name; <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"227\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-underscore-wildcard-example.png\" alt=\"SQL Server LIKE underscore wildcard example\" class=\"wp-image-311\"\/><\/figure>\n\n\n\n<p>The pattern <code>_u%<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first underscore character ( <code>_<\/code>) matches any single character.<\/li>\n\n\n\n<li>The second letter <code>u<\/code> matches the letter u exactly.<\/li>\n\n\n\n<li>The third character <code>%<\/code> matches any sequence of characters.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-like-operator-with-the-list-of-characters-wildcard-example'>3) Using the LIKE operator with the [list of characters] wildcard example <a href=\"#3-using-the-like-operator-with-the-list-of-characters-wildcard-example\" class=\"anchor\" id=\"3-using-the-like-operator-with-the-list-of-characters-wildcard-example\" title=\"Anchor for 3) Using the LIKE operator with the [list of characters] wildcard example\">#<\/a><\/h3>\n\n\n\n<p>The square brackets with a list of characters e.g., <code>[ABC]<\/code> represents a single character that must be one of the characters specified in the list.<\/p>\n\n\n\n<p>For example, the following query returns the customers where the first character in the last name is <code>Y<\/code> or <code>Z<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'&#91;YZ]%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    last_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"214\" height=\"190\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-character-list-example.png\" alt=\"SQL Server LIKE character list example\" class=\"wp-image-314\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='4-using-the-like-operator-with-the-character-character-wildcard-example'>4) Using the LIKE operator with the [character-character] wildcard example <a href=\"#4-using-the-like-operator-with-the-character-character-wildcard-example\" class=\"anchor\" id=\"4-using-the-like-operator-with-the-character-character-wildcard-example\" title=\"Anchor for 4) Using the LIKE operator with the [character-character] wildcard example\">#<\/a><\/h3>\n\n\n\n<p>The square brackets with a character range e.g., <code>[A-C]<\/code> represent a single character that must be within a specified range.<\/p>\n\n\n\n<p>For example, the following query finds the customers where the first character in the last name is the letter in the range <code>A<\/code> through <code>C<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'&#91;A-C]%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"226\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-range-example.png\" alt=\"SQL Server LIKE range example\" class=\"wp-image-310\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='5-using-the-like-operator-with-the-character-list-or-range-wildcard-example'>5) Using the LIKE operator with the [^Character List or Range] wildcard example <a href=\"#5-using-the-like-operator-with-the-character-list-or-range-wildcard-example\" class=\"anchor\" id=\"5-using-the-like-operator-with-the-character-list-or-range-wildcard-example\" title=\"Anchor for 5) Using the LIKE operator with the [^Character List or Range] wildcard example\">#<\/a><\/h3>\n\n\n\n<p>The square brackets with a caret sign (^) followed by a range e.g., <code>[^A-C]<\/code> or character list e.g., <code>[ABC]<\/code> represent a single character that is not in the specified range or character list.<\/p>\n\n\n\n<p>For example, the following query returns the customers where the first character in the last name is not the letter in the range <code>A<\/code> through <code>X<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    last_name <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'&#91;^A-X]%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    last_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"208\" height=\"190\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-caret-example.png\" alt=\"SQL Server LIKE caret example\" class=\"wp-image-313\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='6-using-the-not-like-operator-example'>6) Using the NOT LIKE operator example <a href=\"#6-using-the-not-like-operator-example\" class=\"anchor\" id=\"6-using-the-not-like-operator-example\" title=\"Anchor for 6) Using the NOT LIKE operator example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>NOT LIKE<\/code> operator to find customers where the first character in the first name is not the letter&nbsp;<code>A<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    customer_id,\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    first_name <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'A%'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"201\" height=\"205\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-NOT-LIKE-example.png\" alt=\"SQL Server NOT LIKE example\" class=\"wp-image-312\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='7-using-the-like-operator-with-escape-example'>7) Using the LIKE operator with ESCAPE example <a href=\"#7-using-the-like-operator-with-escape-example\" class=\"anchor\" id=\"7-using-the-like-operator-with-escape-example\" title=\"Anchor for 7) Using the LIKE operator with ESCAPE example\">#<\/a><\/h3>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-create-table\/\">create a new table<\/a> for the demonstration:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> sales.feedbacks (\n  feedback_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span>(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">1<\/span>) PRIMARY <span class=\"hljs-keyword\">KEY<\/span>, \n  <span class=\"hljs-keyword\">comment<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert\/\">insert some rows<\/a> into the <code>sales.feedbacks<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> sales.feedbacks(<span class=\"hljs-keyword\">comment<\/span>)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Can you give me 30% discount?'<\/span>),\n      (<span class=\"hljs-string\">'May I get me 30USD off?'<\/span>),\n      (<span class=\"hljs-string\">'Is this having 20% discount today?'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\">query data<\/a> from the <code>sales.feedbacks<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> sales.feedbacks;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"261\" height=\"75\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-sample-table.png\" alt=\"SQL Server LIKE - sample table\" class=\"wp-image-2180\"\/><\/figure>\n\n\n\n<p>If you want to search for <code>30%<\/code> in the <code>comment<\/code> column, you may come up with a query like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n   feedback_id,\n   <span class=\"hljs-keyword\">comment<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> \n   sales.feedbacks\n<span class=\"hljs-keyword\">WHERE<\/span> \n   <span class=\"hljs-keyword\">comment<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%30%'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"251\" height=\"61\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-without-ESCAPE-clause.png\" alt=\"SQL Server LIKE without ESCAPE clause\" class=\"wp-image-2181\"\/><\/figure>\n\n\n\n<p>The query returns comments that contain 30% and 30 USD, which is not what we expected.<\/p>\n\n\n\n<p>To address this issue, you can use the <code>ESCAPE<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n   feedback_id, \n   <span class=\"hljs-keyword\">comment<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> \n   sales.feedbacks\n<span class=\"hljs-keyword\">WHERE<\/span> \n   <span class=\"hljs-keyword\">comment<\/span> <span class=\"hljs-keyword\">LIKE<\/span> <span class=\"hljs-string\">'%30!%%'<\/span> ESCAPE <span class=\"hljs-string\">'!'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"257\" height=\"40\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-LIKE-with-ESCAPE-clause.png\" alt=\"SQL Server LIKE with ESCAPE clause\" class=\"wp-image-2182\"\/><\/figure>\n\n\n\n<p>In this query, the\u00a0 <code>ESCAPE<\/code> clause specified that the character <code>!<\/code> is the escape character. <\/p>\n\n\n\n<p>It instructs the <code>LIKE<\/code> operator to treat the <code>%<\/code> character as a literal string instead of a wildcard. Note that without the <code>ESCAPE<\/code> clause, the query would return an empty result set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>LIKE<\/code> operator to check if a value matches a specified pattern.<\/li>\n\n\n\n<li>Use the <code>NOT<\/code> operator to negate the <code>LIKE<\/code> operator.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"301\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/\"\n\t\t\t\tdata-post-title=\"SQL Server LIKE Operator\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"301\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/\"\n\t\t\t\tdata-post-title=\"SQL Server LIKE Operator\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":100,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-301","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server LIKE Operator<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server LIKE Operator\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-02T08:50:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/\",\"name\":\"SQL Server LIKE Operator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/customers.png\",\"datePublished\":\"2018-04-29T04:23:40+00:00\",\"dateModified\":\"2024-03-02T08:50:56+00:00\",\"description\":\"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/customers.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/customers.png\",\"width\":181,\"height\":231,\"caption\":\"customers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-like\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Basics\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server LIKE Operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server LIKE Operator","description":"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server LIKE Operator","og_description":"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-03-02T08:50:56+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/","name":"SQL Server LIKE Operator","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png","datePublished":"2018-04-29T04:23:40+00:00","dateModified":"2024-03-02T08:50:56+00:00","description":"This tutorial shows you how to use the SQL Server LIKE operator to check whether a character string matches a specified pattern.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png","width":181,"height":231,"caption":"customers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-like\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Basics","item":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/"},{"@type":"ListItem","position":3,"name":"SQL Server LIKE Operator"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":3,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/301\/revisions"}],"predecessor-version":[{"id":3410,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/301\/revisions\/3410"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/100"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}