I am not getting the exact record if string has underscore is a first char Example:
declare @name nvarchar(max)
set @name='_#@#_1'
SELECT Name from Emp where Name like @name + '%'
Expected Output: It should return a single row as per table records (_#@#_123) but returning below records
_#@#_123
@#@#@123
@#@#_123
_#@#_123
3条答案
按热度按时间w8rqjzmb1#
Since underscore is a special character, you'll have to escape it using the character of your choice. Let's use a backslash
\
. You'll have to both 1) escape it in your data and 2) add theESCAPE
clause:2jcobegt2#
As mentioned above, the result of your query is correct since the underscore is the wildcard for a single character. However, you might want to try the following which basically does the same as your
LIKE ... + '%'
:p8h8hvxi3#
Wrap underscore using brackets:
Try working demo