用于将括号内的搜索项封闭起来的TSQL语法(TSQL syntax for enclosing search terms within brackets)

编程入门 行业动态 更新时间:2024-10-27 14:23:30
用于将括号内的搜索项封闭起来的TSQL语法(TSQL syntax for enclosing search terms within brackets)

我相信[和]在使用TSQL中的LIKE子句时是特殊字符(SQlserver 2005,如果它很重要)。 我如何转义存储过程中的搜索词,我在下面做了,但即使在存在的时候也没有返回有效的记录

SELECT * FROM Table WHERE Column LIKE '%['+ @searchedTerm +']'

那么在搜索上面的时候哪个是有效的呢?

I believe [ and ] are special characters when using with LIKE clause in TSQL( SQlserver 2005 if it matters). How do i escape the search term in the stored procedure, i did below but does not return valid records even while exists

SELECT * FROM Table WHERE Column LIKE '%['+ @searchedTerm +']'

So which is the valid thing to do, when searching like above??

最满意答案

你需要像这样跳过开头(不是闭幕)括号:

LIKE '%[[]' + @searchedTerm + ']'

LIKE的MSDN页面有一个“使用通配符作为文字”

编辑,评论之后

LIKE '[[]cap]%'搜索包含字符串[cap]的名称 LIKE '[wel]%'搜索包含字母w , e或l 之一的名称

You need to escape the opening (not the closing) bracket like this:

LIKE '%[[]' + @searchedTerm + ']'

The MSDN page for LIKE has a section "Using Wildcard Characters As Literals"

Edit, after comment

LIKE '[[]cap]%' searches for a name containing the string [cap] LIKE '[wel]%' searches for a name containing one of the letters w, e or l

更多推荐

本文发布于:2023-07-28 21:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309452.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   括号内   TSQL   syntax   brackets

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!