user3798811
user3798811

Reputation: 523

Case statement with nulls

I am writing a case statement to return true or false if a record is returned in my query. A lot number is returned it is True and when no lot number returned it is false. I keep getting just a blank when no record is returned. The true side works well. I would like it to return false when no record is returned. I am sure it is something simple. Thanks in advance.

declare  @lotnum  nvarchar(100)
set  @lotnum ='3113'


select 
case when LOT_0 = '17-WA411-014' then 'True' else 'False' end 
from  [x3v7].[LIVE].[STOLOT]
where LOT_0 ='17-WA411-014'
group by LOT_0

Upvotes: 0

Views: 114

Answers (3)

Man of Joy
Man of Joy

Reputation: 9

IF EXISTS(SELECT 1
from [x3v7].[LIVE].[STOLOT] where LOT_0 ='17-WA411-014') select case when LOT_0 = '17-WA411-014' then 'True' else 'False' end from [x3v7].[LIVE].[STOLOT] where LOT_0 ='17-WA411-014' group by LOT_0 ELSE SELECT 'False'

Hope below one helps,

Upvotes: 0

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

Just remove your where clause :

So, it would be :

select LOT_0, (case when LOT_0 = '17-WA411-014' 
                    then 'True' else 'False' 
               end) 
from  [x3v7].[LIVE].[STOLOT]
group by LOT_0;

Upvotes: 0

D-Shih
D-Shih

Reputation: 46219

Try to remove where LOT_0 ='17-WA411-014'

select case when LOT_0 = '17-WA411-014' then 'True' else 'False' end 
from  [x3v7].[LIVE].[STOLOT]
group by LOT_0

Upvotes: 1

Related Questions