JOZO
JOZO

Reputation: 89

Get results with wildcard

I am returning @ChangeStatus variable with 'Y', 'N', Null and need to show data based on that however column has different string than variable

declare @ChangeStatus char(1) = 'Y'
select * from Application 
where Changes = (case when @ChangeStatus is null then Changes end)
and Changes like (case when @ChangeStatus = 'Y' then '%'end)
and Changes like (case when @ChangeStatus = 'N' then NULL end)

however no data shown

Upvotes: 1

Views: 60

Answers (1)

Martin Brown
Martin Brown

Reputation: 25329

I suspect you want to use a combination of AND and OR with some brackets thrown in something like this:

select * 
from Application 
where 
    @ChangeStatus is null OR
    (@ChangeStatus = 'Y' AND Changes like '%') OR
    (@ChangeStatus = 'N' AND Changes is null)

Upvotes: 3

Related Questions