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]
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
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!
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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. 
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

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?
Avatar of Mark Wills
Mark Wills
Flag of Australia image

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.
Avatar of ste5an
Flag of Germany image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo