question

bigdog4242 avatar image
bigdog4242 asked

case if variable doesn't match display all

I have an issue where if the results match then display the results but if it doesn't match display all the values. Please see below. Where DRWG_CLASS = 8 DRWG_CLASS values 1 2 3 4 5 8 So in the where if DRWG_CLASS = 8 then 8 will appear but if DRWG_CLASS = 6 then 1,2,3,4,5,8 will appear. Thanks in advance for your help!
case
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I'm sorry, but I really don't understand the context of this question. Is this within a T-SQL statement? If so, what does the statement look like?
0 Likes 0 ·

1 Answer

·
iainrobertson avatar image
iainrobertson answered
create table #sample (DRWG_CLASS int) go insert #sample values (1),(2),(3),(4),(5),(8) go -- use a union query declare @check_value int set @check_value = 5 select * from #sample where DRWG_CLASS = @check_value union all select * from #sample where @check_value = case when exists ( select 'in set' from #sample where DRWG_CLASS = @check_value ) then @check_value + 1 else @check_value end -- when @check_value = 5 (i.e. exists), this evaluates to 5 = 6 - no rows returned -- when check_value = 6 (i.e. does not exist), this evaluates to 6 = 6 - all rows returned
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

This works perfect! Thank you so much!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.