LIKE not working in TSQL

强颜欢笑 提交于 2019-12-01 08:40:43

问题


Consider this TSQL:

declare @b varchar(100)

set @b = 'BANK-41'

IF @b LIKE 'BANK_%'
BEGIN
    print 'Wrong Matching'
END

Why does the TSQL match the string "BANK-" and "BANK_"?


回答1:


In TSQL the underscore is a wildcard representing a single char.

In order to escape in you need to wrap it with square brackets, like this:

'BANK[_]%'

See this page: http://www.dirigodev.com/blog/web-development-execution/escaping-percent-and-underscore-characters-in-t-sql-like-clause/




回答2:


An underscore in SQL Server is reserved for a wild card character, I think. You need to escape it.I think you can put it in brackets:

%[_]%



回答3:


You need to escape the "_". It is a special character for the Like statement. You can enclose it in [].

See LIKE (Transact-SQL).



来源:https://stackoverflow.com/questions/19598180/like-not-working-in-tsql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!