Link to home
Start Free TrialLog in
Avatar of Tim Mullady
Tim MulladyFlag for United States of America

asked on

SQL Case statement not return under FALSE

Any ideas why the Else condition is not returning the [Parse_Row2] value?  It does return the True condition but not the False condition.  


,Case When [Parse_Row4] is not null
      Then [Parse_Row4]
      Else [Parse_Row2]
      End
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

From what you posted the only way it will never get to the else is if [Parse_Row4] is NEVER null.

Don't confuse empty strings as nulls.  Possibly:  Case When trim([Parse_Row4]) is not null

We'll need more information.  Sample data and expected results would help a LOT!
Just a thought..   ISNULL([Parse_Row4], [Parse_Row2])   does the same thing.

And of course slightwv is correct regarding...
  • empty strings ' ', which are still a value, different from NULL which is the absence of a value. 
  • Give us your full T-SQL statement and some sample data. 
the easiest way is to compare the field values side by side:

[Parse_Row4] , Parse_Row2] ,
Case When [Parse_Row4] is not null  Then [Parse_Row4] Else [Parse_Row2]   End 

Open in new window


what's your expected result for the False condition?

could the False condition returns the same value as True condition as well? like NULL, etc?
I tend to agree with the experts above in so much as checking for NULL is often not enough.

I would be inclined to check LEN

ie  case when len(isnull(Parse_ROW4,'')) > 0 then Parse_ROW4 else Parse_ROW2 end as result

Just to test....

create table #EE_TEST (Parse_ROW4 varchar(20), Parse_ROW2 varchar(20))

insert #EE_TEST values (null,'2a')
insert #EE_TEST values ('4b','2b')
insert #EE_TEST values (' ','2c')

select Parse_ROW4, Parse_ROW2, len(Parse_ROW4) as Len_Parse_ROW4,
       case when len(isnull(Parse_ROW4,'')) > 0 then Parse_ROW4 else Parse_ROW2 end as result
from #EE_TEST

Open in new window

Or, am I missing something else ? It almost sounds like it is only ever returning the TRUE condition, meaning Parse_ROW4 is never null.... In which case are there other conditions in you query that *might* be impacting your case condition.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial