This question already has an answer here:
这个问题在这里已有答案:
- SQL Server Escape an Underscore 3 answers
- SQL Server逃脱Underscore 3的答案
I want to select the rows that have a '_' character in a given column from SQL. when I try to run this query, it returns all the rows in the table
我想从SQL中选择给定列中具有“_”字符的行。当我尝试运行此查询时,它返回表中的所有行
SELECT * FROM [Attribute] where LibID=26 and Name like '%_%'
but the following query yields what I need
但以下查询产生了我需要的东西
SELECT * FROM [Attribute] where LibID=26 and CHARINDEX('_', Name) > 0
why my first query is not returning expected result. Maybe I am missing some information/knowledge of SQL.
为什么我的第一个查询没有返回预期的结果。也许我错过了一些SQL的信息/知识。
2 个解决方案
#1
3
The _
(underscore) character is used by the LIKE function to match any character. If you want to use it as an underscore, you need to escape it:
LIKE函数使用_(下划线)字符来匹配任何字符。如果要将其用作下划线,则需要将其转义:
SELECT *
FROM [Attribute]
where LibID=26
and Name like '%[_]%'
#2
1
The underscore is a wildcard character in a LIKE query. It indicates any single character.
下划线是LIKE查询中的通配符。它表示任何单个字符。
To use the LIKE syntax, use:
要使用LIKE语法,请使用:
SELECT * FROM [Attribute] where LibID=26 and Name like '%[_]%'
SELECT * FROM [Attribute]其中LibID = 26且名称类似'%[_]%'
#1
3
The _
(underscore) character is used by the LIKE function to match any character. If you want to use it as an underscore, you need to escape it:
LIKE函数使用_(下划线)字符来匹配任何字符。如果要将其用作下划线,则需要将其转义:
SELECT *
FROM [Attribute]
where LibID=26
and Name like '%[_]%'
#2
1
The underscore is a wildcard character in a LIKE query. It indicates any single character.
下划线是LIKE查询中的通配符。它表示任何单个字符。
To use the LIKE syntax, use:
要使用LIKE语法,请使用:
SELECT * FROM [Attribute] where LibID=26 and Name like '%[_]%'
SELECT * FROM [Attribute]其中LibID = 26且名称类似'%[_]%'