用于将括号内的搜索项封闭起来的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更多推荐
发布评论