<p>How do I escape the underscore character?</p>

<p>I am writing something like the following where clause and want to be able to find actual entries with _d at the end.</p>

<pre><code>Where Username Like '%_d'
</code></pre>

[{'answer_id': 5822, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 10, 'selected': True, 'text': '<p><a href="https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15#using-wildcard-characters-as-literals" rel="noreferrer">T-SQL Reference for LIKE</a>:</p>\n\n<blockquote>\n  <p>You can use the wildcard pattern matching characters as literal characters. To use a wildcard character as a literal character, enclose the wildcard character in brackets. The following table shows several examples of using the LIKE keyword and the [ ] wildcard characters.</p>\n</blockquote>\n\n<p>For your case:</p>\n\n<pre><code>... LIKE \'%[_]d\'\n</code></pre>\n'}, {'answer_id': 10415670, 'author': 'Gerardo Lima', 'author_id': 394042, 'author_profile': 'https://Stackoverflow.com/users/394042', 'pm_score': 8, 'selected': False, 'text': '<p>Obviously @Lasse solution is right, but there\'s another way to solve your problem: T-SQL operator <code>LIKE</code> defines the optional <a href="http://msdn.microsoft.com/en-us/library/ms179859%28v=sql.90%29.aspx" rel="noreferrer">ESCAPE</a> clause, that lets you declare a character which will escape the next character into the pattern.</p>\n\n<p>For your case, the following WHERE clauses are equivalent:</p>\n\n<pre><code>WHERE username LIKE \'%[_]d\';            -- @Lasse solution\nWHERE username LIKE \'%$_d\' ESCAPE \'$\';\nWHERE username LIKE \'%^_d\' ESCAPE \'^\';\n</code></pre>\n'}, {'answer_id': 50650186, 'author': 'Tek Mailer', 'author_id': 5697368, 'author_profile': 'https://Stackoverflow.com/users/5697368', 'pm_score': 3, 'selected': False, 'text': "<p>These solutions totally make sense. Unfortunately, neither worked for me as expected. Instead of trying to hassle with it, I went with a work around:</p>\n<pre><code>select *\nfrom information_schema.columns \nwhere replace(table_name,'_','!') not like '%!%'\norder by table_name\n</code></pre>\n"}, {'answer_id': 52702908, 'author': 'Arnór Barkarson', 'author_id': 7436253, 'author_profile': 'https://Stackoverflow.com/users/7436253', 'pm_score': 2, 'selected': False, 'text': "<p>This worked for me, just use the escape\n<code>'%\\_%'</code></p>\n"}, {'answer_id': 56812000, 'author': 'wolverine87', 'author_id': 7885982, 'author_profile': 'https://Stackoverflow.com/users/7885982', 'pm_score': 1, 'selected': False, 'text': "<p>None of these worked for me in SSIS v18.0, so I would up doing something like this:<br /><br />\n<code>WHERE CHARINDEX('_', thingyoursearching) &lt; 1</code><br /><br />..where I am trying to ignore strings with an underscore in them. If you want to find things that have an underscore, just flip it around:<br /><br />\n<code>WHERE CHARINDEX('_', thingyoursearching) &gt; 0</code></p>\n"}, {'answer_id': 64153299, 'author': 'Freddy Madsen', 'author_id': 12632795, 'author_profile': 'https://Stackoverflow.com/users/12632795', 'pm_score': 3, 'selected': False, 'text': "<p>I had a similar issue using like pattern <code>'%_%'</code> did not work - as the question indicates :-)</p>\n<p>Using <code>'%\\_%'</code> did not work either as this first <code>\\</code> is interpreted &quot;before the like&quot;.</p>\n<p>Using <code>'%\\\\_%'</code> works. The <code>\\\\</code> (double backslash) is first converted to single <code>\\</code> (backslash) and then used in the like pattern.</p>\n"}, {'answer_id': 66069453, 'author': 'Alex C.', 'author_id': 2349128, 'author_profile': 'https://Stackoverflow.com/users/2349128', 'pm_score': 3, 'selected': False, 'text': "<p>Adding <code>[ ]</code> did the job for me</p>\n<pre><code>like '%[\\\\_]%'\n</code></pre>\n"}, {'answer_id': 71712662, 'author': 'Zosoled', 'author_id': 12221876, 'author_profile': 'https://Stackoverflow.com/users/12221876', 'pm_score': 0, 'selected': False, 'text': '<p>Adding to <a href="https://stackoverflow.com/a/10415670/12221876">Gerardo Lima\'s answer</a>, I was having problems when trying to use backslash as my escape character for the ESCAPE clause. This caused issues:</p>\n<pre><code>SELECT * FROM table WHERE email LIKE \'%@%\\_%\' ESCAPE \'\\\'\n</code></pre>\n<p>It was resolved by switching to an exclamation point. This worked:</p>\n<pre><code>SELECT * FROM table WHERE email LIKE \'%@%!_%\' ESCAPE \'!\'\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/5821', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/383/']