开发者

Like used with parameter don't work correctly

开发者 https://www.devze.com 2023-03-29 09:03 出处:网络
This script should only pick the rows with brickurl = \'http://www.bol.com/something\' But it returns nearly all 30 rows, even with totally other texts 开发者_开发百科in it

This script should only pick the rows with brickurl = 'http://www.bol.com/something' But it returns nearly all 30 rows, even with totally other texts 开发者_开发百科in it

DECLARE @urlpart nvarchar
set @urlpart='bol.com'
SET @urlpart = '%'+@urlpart + '%'

SELECT * FROM Brick
WHERE BrickUrl like @urlpart

written without parameter like this:

SELECT * FROM brick
WHERE BrickUrl like '%bol.com%'

returns the 6 rows containing bol.com correctly.

I can't figure out what the reason is, or what I do wrong, do you see it?


Try this:

DECLARE @urlpart nvarchar
set @urlpart='bol.com'
SET @urlpart = '%'+@urlpart + '%'
select @urlpart

Result:

(No column name)
%

and then you should try this instead

DECLARE @urlpart nvarchar(128)
set @urlpart='bol.com'
SET @urlpart = '%'+@urlpart + '%'
select @urlpart

Result;

(No column name)
%bol.com%

When not specifying the size of nvarchar you will get size 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号