为什么 T-SQL 是“LIKE”?运算符没有像我认为应该的那样评估这个表达式?

发布于 2024-11-25 18:21:17 字数 908 浏览 3 评论 0原文

我试图通过确保变量的值以括号“[”为前缀来错误捕获 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不爱素颜 2024-12-02 18:21:17

我相信这可能是因为“[”实际上是 LIKE 运算符语法的一部分,如下定义: http://msdn.microsoft.com/en-us/library/ms179859.aspx

您需要定义一个转义字符来转义 [,如下所示:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(@thing NOT LIKE '\[%' ESCAPE '\' )
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing

另一种解决方案如下:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(LEFT(@thing,1) <> '[') --If the value does not start with [ then add it
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing

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:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(@thing NOT LIKE '\[%' ESCAPE '\' )
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing

An alternative solution would be the following:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
IF(LEFT(@thing,1) <> '[') --If the value does not start with [ then add it
BEGIN
SET @thing = '[' + @thing
END
PRINT @thing
涙—继续流 2024-12-02 18:21:17

要使其正常工作,请将检查更改为

IF (SUBSTRING(@thing, 1,1) != '[')

The like not work is because [ is a Special char in like.就像 % 一样。请参阅此处

To get it working change your check to

IF (SUBSTRING(@thing, 1,1) != '[')

The reason why the like is not working is because [ is a special char in like. just like % is. See here

白馒头 2024-12-02 18:21:17

括号字符([]是 T-SQL 中的特殊通配符。要搜索这些文字字符,您需要转义这些字符(表明您想要搜索这些文字字符,而不是将它们用作通配符)。使用 ESCAPE 来执行此操作,如下所示:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
-- pick an escape character you won't see in your content
IF(@thing NOT LIKE '![' + '%' ESCAPE '!')
BEGIN
  SET @thing = '[' + @thing
END
PRINT @thing

这将打印 [55555

来自 MSDN

您可以搜索包含一个或多个特殊通配符的字符串...要搜索百分号作为字符而不是通配符,必须提供 ESCAPE 关键字和转义字符。例如,示例数据库包含一个名为 comment 的列,其中包含文本 30%。要搜索 comment 列中任意位置包含字符串 30% 的任何行,请指定 WHERE 子句,例如 WHERE comment LIKE '%30!%%'转义“!”

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). Use ESCAPE to do this, like so:

DECLARE @thing nvarchar(20)
SET @thing = '[55555'
-- pick an escape character you won't see in your content
IF(@thing NOT LIKE '![' + '%' ESCAPE '!')
BEGIN
  SET @thing = '[' + @thing
END
PRINT @thing

This prints [55555.

From MSDN:

You can search for character strings that include one or more of the special wildcard characters... To search for the percent sign as a character instead of as a wildcard character, the ESCAPE keyword and escape character must be provided. For example, a sample database contains a column named comment that contains the text 30%. To search for any rows that contain the string 30% anywhere in the comment column, specify a WHERE clause such as WHERE comment LIKE '%30!%%' ESCAPE '!'.

薄暮涼年 2024-12-02 18:21:17

您必须转义特殊字符(括号、单引号等)。在这种情况下,您可以这样做:

LIKE '[['

编辑:

PS -- [ 是一个特殊字符,因为它可用于通配符,如下所示: LIKE '[0-9]' 进行模式匹配。 (在这种情况下,匹配就像正则表达式——0 到 9 之间的任何数字。

You have to escape special characters (brackets, single quotes, etc.). In this case, you could do this:

LIKE '[['

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文