Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Escape an Underscore

Tags:

sql-server

How do I escape the underscore character?

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

Where Username Like '%_d' 
like image 578
GateKiller Avatar asked Aug 08 '08 11:08

GateKiller


People also ask

How do I escape an underscore in SQL?

I want to write a query to display all the row which contains an (_) underscore. The escape character makes SQL treat whatever follows it as a literal character. Put square-brackets around the underscore in the Like statement.

Can you use _ in SQL?

To broaden the selections of a structured query language (SQL-SELECT) statement, two wildcard characters, the percent sign (%) and the underscore (_), can be used.

How do you escape a character in SQL Server?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

How do I match an underscore in SQL?

If you are searching for an underscore then escape it with a '\' so you would search for' \_'. When matching SQL patterns, you can use these symbols as placeholders: % (percent) to substitute for zero or more characters, or _ (underscore) to substitute for one single character.


1 Answers

T-SQL Reference for LIKE:

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.

For your case:

... LIKE '%[_]d' 
like image 169
Lasse V. Karlsen Avatar answered Nov 04 '22 23:11

Lasse V. Karlsen