Matt
Matt

Reputation: 273

SQL Server : conditional case in where clause

I would like to write the following as a CASE statement in my WHERE clause, but I'm unsure how to write it.

if @Result = '' then 
    [Result] = @Result
else
    [Result] like @Result + '%'
endif

Upvotes: 2

Views: 18060

Answers (2)

Nithesh Narayanan
Nithesh Narayanan

Reputation: 11755

Try this. This is what exactly same as your if else statement

WHERE [Result] LIKE (CASE @Result WHEN '' THEN @Result ELSE @Result + '%' END)

Upvotes: 0

codingbadger
codingbadger

Reputation: 43974

Something like this would work.

Declare @Result varchar(100)

Set @Result  =''

Select * 
From    dbo.TABLE
Where [Result] Like Case 
            When @Result = '' Then @Result
            Else @result + '%'
            End

Upvotes: 4

Related Questions