开发者

How to use a LIKE statement inside an IF statement?

开发者 https://www.devze.com 2022-12-27 13:07 出处:网络
I need to be able to check if a variable has a certain string of text inside it. Here\'s what I have now:

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

精彩评论

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

关注公众号