开发者

Like used with parameter don't work correctly

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜