I have following table structure in my DB
ID Name
--------------------------
ID_1 Name1
ID1 Name2
when I execute the following query
SELECT * FROM tblNames
WHERE ID LIKE '_1'
I get both records selected…
any suggestions how to get rid off it??
An underscore (
_) is a special character in LIKE expressions that matches a single character.To match an actual underscore, you need to escape it:
Here I’m telling SQL Server to consider backslash an escape character, and using it to escape the underscore.
Alternatively, you can represent the underscore as a character range with a single character in it – it will be interpreted literally in this case:
Which is a bit more succinct.
Reference: MSDN.