开发者

How to use a LIKE statement inside an IF statement?

I need to be able to check if a variable has a certain string of text inside it. Here's what I have now:

--This sample always goes to the ELSE block.
IF( @name LIKE '%John%' )
BEGIN
    --do one thing
END
ELSE
BEGIN
    --开发者_如何学编程do the other thing
END


dunno...works for me

declare @name varchar(45)

set @name = 'johnson'

IF( @name LIKE '%John%' )
BEGIN
    print 'like'
END
ELSE
BEGIN
    print 'dislike'
END


The syntax is fine. If it isn't working for you, it could be down to the collation of your database.

If you have a case sensitive collation, then it will NOT match unless the case matches. e.g. if @name is 'Something john said', then a LIKE searching for "John" will not find a match, hence will go to the ELSE.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜