SQL Server : conditional case in where clause

18,051

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
Share:
18,051
Matt
Author by

Matt

Updated on June 04, 2022

Comments

  • Matt
    Matt about 2 years

    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
    
  • Matt
    Matt about 11 years
    bang on mate. Many thanks, I'll accept your answer as soon as it lets me.