Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

Case query returning NULL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fieldmedic - 09 Jul 2008 20:18 GMT
I have the following as part of a query on a SQL 05 server.

patient_name = case patient.middle
    when NULL then patient.first+' '+patient.last
    else patient.first+' '+patient.middle+' '+patient.last
    end,

The object being that if the middle name has a null value that it just
displays the first and last name. Since without the case statement if they
had a NULL middle name, no name would be returned.

The problem is that with the case statement in place the ones that have NULL
in the middle name just return NULL for a value.

Since I'm obviously doing something wrong, I was wondering if you would
point me in the right direction.

Thanks in advance.
DCPS - 09 Jul 2008 20:33 GMT
On Jul 9, 3:18 pm, Fieldmedic <Fieldme...@discussions.microsoft.com>
wrote:
> I have the following as part of a query on a SQL 05 server.
>
[quoted text clipped - 14 lines]
>
> Thanks in advance.

Can you run it like this and avoid the CASE:

patient.first + ISNULL(patient.middle,'') + patient.last
Roy Harvey (SQL Server MVP) - 09 Jul 2008 20:42 GMT
WHEN NULL doesn't work the way you want it too.  NULL is never equal
to NULL, oddly enough.

One alternative:

patient_name = patient.first +
              COALESCE(' ' + patient.middle + ' ', ' ') +
              patient.last

Another:

patient_name = case
    when patient.middle IS NULL
    then patient.first+' '+patient.last
    else patient.first+' '+patient.middle+' '+patient.last
    end,

Roy Harvey
Beacon Falls, CT

>I have the following as part of a query on a SQL 05 server.
>
[quoted text clipped - 14 lines]
>
>Thanks in advance.
Aaron Bertrand [SQL Server MVP] - 09 Jul 2008 20:42 GMT
No need for CASE here at all, I think...

SELECT COALESCE(patient.first, '') + COALESCE(' ' + patient.middle, '') +
COALESCE(' ' + patient.last, '')
FROM table

On 7/9/08 3:18 PM, in article
8ADB5200-AE2F-463E-BAC6-591A67182F0B@microsoft.com, "Fieldmedic"

> I have the following as part of a query on a SQL 05 server.
>
[quoted text clipped - 14 lines]
>
> Thanks in advance.
Fieldmedic - 09 Jul 2008 21:05 GMT
Wow thanks guys, I've tried all of them and they worked. :) So I guess it
just illustrates the point that there's "more than one way to skin a cat."
The first response needs a little tweaking since it doesn't have all the
spaces but that's easy enough to figure. Thanks again!

> No need for CASE here at all, I think...
>
[quoted text clipped - 23 lines]
> >
> > Thanks in advance.
newscorrespondent@charter.net - 10 Jul 2008 20:36 GMT
I suspect the way you coded it SQL is doing an equal compare and of course
nothing including a null ever equals another null. I coded it like you and
with a little variation. The variation works.

DECLARE @Firstname    VARCHAR(255);
DECLARE @Middlename    VARCHAR(255);
DECLARE @Lastname    VARCHAR(255);

SELECT    @Firstname    =    'George'
        ,@Middlename    =    NULL
        ,@Lastname    =    'Washington';
--    returns null
SELECT    CASE    @Middlename
        WHEN    NULL    THEN    @Firstname + ' ' +     @Lastname
        ELSE    @Firstname + ' ' + @Middlename  + ' ' +    @Lastname
        END;
-- returns what you want
SELECT    CASE    WHEN @Middlename IS NULL
        THEN    @Firstname + ' ' +     @Lastname
        ELSE    @Firstname + ' ' + @Middlename  + ' ' +    @Lastname
        END;
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.