为什么 T-SQL 是“LIKE”?运算符没有像我认为应该的那样评估这个表达式?
我试图通过确保变量的值以括号“[”为前缀来错误捕获 T-SQL 变量名称。
下面是我尝试执行此操作的示例:
DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(@thing NOT LIKE '[' + '%') --If the value does not start with [ then add it
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing
上面的示例 PRINT 的 [[55555
请注意,@thing 的原始值以括号“[”为前缀。我原以为 IF 条件会返回 false,因为 "[55555" is LIKE '[' + '%'
为什么 IF 条件不返回 false?而且,更重要的是,我想,检查变量字符串值开头是否存在字符串的正确语法是什么?
编辑 看来括号“[”有一些特殊之处。当我在括号上运行 LIKE 时,它不会执行我期望的操作,但是当我不使用括号时,LIKE 会按照我期望的方式工作。
查看这些示例:
IF('[' NOT LIKE '[')
BEGIN
PRINT '[ is NOT LIKE ['
END
ELSE
BEGIN
PRINT '[ is LIKE ['
END
IF('StackO' NOT LIKE 'StackO')
BEGIN
PRINT 'STACKO is NOT LIKE StackO'
END
ELSE
BEGIN
PRINT 'STACKO is LIKE StackO'
END
这是两个条件的输出:
[ 不像 [
STACKO 与 StackO 类似
I am attempting to error trap a T-SQL variable name by making sure that the value of the variable is prefixed with a bracket "[".
Here's an example of how I am trying to do this:
DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(@thing NOT LIKE '[' + '%') --If the value does not start with [ then add it
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing
The example above PRINT's [[55555
Notice that the original value of @thing was prefixed with the bracket "[". I was expecting the IF condition would have returned false since "[55555" is LIKE '[' + '%'
Why is the IF condition not returning false? And, more importantly I suppose, what is the correct syntax to check for the existence of a string that occurs at the beginning of a variable string value?
EDIT
It appears as there is something special about the bracket "[". When I run LIKE on a bracket it doesn't do what I expect, but when I don't use a bracket the LIKE works the way I expect.
Check out these examples:
IF('[' NOT LIKE '[')
BEGIN
PRINT '[ is NOT LIKE ['
END
ELSE
BEGIN
PRINT '[ is LIKE ['
END
IF('StackO' NOT LIKE 'StackO')
BEGIN
PRINT 'STACKO is NOT LIKE StackO'
END
ELSE
BEGIN
PRINT 'STACKO is LIKE StackO'
END
Here's the output of the two conditions:
[ is NOT LIKE [
STACKO is LIKE StackO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这可能是因为“[”实际上是 LIKE 运算符语法的一部分,如下定义: http://msdn.microsoft.com/en-us/library/ms179859.aspx
您需要定义一个转义字符来转义 [,如下所示:
另一种解决方案如下:
I believe it may be because '[' is actually part of the LIKE operators syntax, as defined here: http://msdn.microsoft.com/en-us/library/ms179859.aspx
You need to define an escape character to escape the [, like this:
An alternative solution would be the following:
要使其正常工作,请将检查更改为
The like not work is because [ is a Special char in like.就像 % 一样。请参阅此处
To get it working change your check to
The reason why the like is not working is because [ is a special char in like. just like % is. See here
括号字符(
[
和]
)是 T-SQL 中的特殊通配符。要搜索这些文字字符,您需要转义这些字符(表明您想要搜索这些文字字符,而不是将它们用作通配符)。使用ESCAPE
来执行此操作,如下所示:这将打印
[55555
。来自 MSDN:
Bracket characters (
[
and]
) are special wildcard characters in T-SQL. To search for those literal characters, you'll want to escape those characters (indicate that you want to search for those literal characters, rather than employing them as wildcards). UseESCAPE
to do this, like so:This prints
[55555
.From MSDN:
您必须转义特殊字符(括号、单引号等)。在这种情况下,您可以这样做:
编辑:
PS -- [ 是一个特殊字符,因为它可用于通配符,如下所示:
LIKE '[0-9]'
进行模式匹配。 (在这种情况下,匹配就像正则表达式——0 到 9 之间的任何数字。You have to escape special characters (brackets, single quotes, etc.). In this case, you could do this:
EDIT:
PS -- [ is a special character because it can be used for wildcards, like this:
LIKE '[0-9]'
to do pattern-matching. (In this case, the match is like a regex -- any digit between 0 and 9.