本文翻译自:SQL Server Escape an Underscore
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子句的内容,并希望能够在末尾找到_d的实际条目。
Where Username Like '%_d'参考:https://stackoom.com/question/1Vt/SQL-Server转义下划线
Obviously @Lasse solution is right, but there's another way to solve your problem: T-SQL operator LIKE defines the optional ESCAPE clause, that lets you declare a character which will escape the next character into the pattern. 显然,@ Lasse解决方案是正确的,但是还有另一种解决问题的方法:T-SQL运算符LIKE定义了可选的ESCAPE子句,该子句使您可以声明一个字符,该字符将下一个字符转义到模式中。
For your case, the following WHERE clauses are equivalent: 对于您的情况,以下WHERE子句是等效的:
WHERE username LIKE '%[_]d'; -- @Lasse solution WHERE username LIKE '%$_d' ESCAPE '$'; WHERE username LIKE '%^_d' ESCAPE '^';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: 我没有尝试麻烦,而是进行了以下工作:
select * from information_schema.columns where replace(table_name,'_','!') not like '%!%' order by table_name这对我有用,只需使用转义符'%\\_%'
None of these worked for me in SSIS v18.0, so I would up doing something like this: 这些都不适合我在SSIS v18.0中使用,因此我会做这样的事情: WHERE CHARINDEX('_', thingyoursearching) < 1 ..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: 如果要查找带有下划线的内容,只需翻转一下即可: WHERE CHARINDEX('_', thingyoursearching) > 0
T-SQL Reference for LIKE for SQL Server 2000 : 适用于SQL Server 2000的LIKE的T-SQL参考 :
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 table shows several examples of using the LIKE keyword and the [ ] wildcard characters. 下表显示了使用LIKE关键字和[]通配符的几个示例。
For your case: 对于您的情况:
... LIKE '%[_]d'