Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE not working in TSQL

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_"?

like image 476
user2921359 Avatar asked Dec 29 '25 16:12

user2921359


1 Answers

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/

like image 149
Yochai Avatar answered Dec 31 '25 08:12

Yochai