Solved

How to select multiple columns and return the one which is not empty

Posted on 2012-04-09
7
305 Views
Last Modified: 2012-08-14
I'm working on a SQL query for an application and there are 3 fields, first, last and company name.  If the first and last name are empty then I want to return the company name and if the first and last name have data, I want to return that information.  I was originally using the following statement:

SELECT c.ID, coalesce (c.LastName + ', ' + c.FirstName, c.Company) AS Name
FROM Customer as c
ORDER BY LastName

I then realized when I commit data to the database from my application which is written in C#, it's not saving a Null value to the database when a field is blank so coalesce does not work for my purpose.  Is there a similar way to achieve the above query that works on empty strings?

Thanks
0
Comment
Question by:obb-taurus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:barlet
ID: 37825768
try to convert <empty_fields> to NULL with the following command
UPDATE your_Table
set [your_Column] = NULLIF([your_Column], '')

Open in new window

and then use coalesce?!
0
 

Author Comment

by:obb-taurus
ID: 37825803
Yes, that would work but if the data from my application but if the data is not being saved as NULl, I will have to run this update every time data is saved from my application which is not very efficient.  What I have come up with is as follows, not sure if this is a good way to accomplish what I'm trying to do.

SELECT c.ID,
CASE
   WHEN c.FirstName IS NULL AND c.LastName IS NULL
         THEN c.Company
   WHEN c.FirstName = '' AND c.LastName = ''
         THEN c.Company
         ELSE
             c.FirstName + ', ' + c.LastName
END
   AS Name
FROM Customer as c
ORDER BY LastName;
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37825875
Not sure you got it quite right, i.e. what if only one of the names is an empty string.  Don't know what you want back in that case, but try this;

SELECT c.ID,
coalesce(nulllif(c.lastname+', '+c.firstname,', ')
         ,nullif(c.lastname,'')
         ,nullif(c.firstname,'')
         ,c.Company) AS Name
FROM Customer as c
ORDER BY LastName;
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 37826023
try something like this:

case when isnull(lastname,'')+', '+isnull(firstname,'')=', ' then companyname else isnull(lastname,'')+', '+isnull(firstname,'') end as name
0
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 37826359
Slight correction:

SELECT c.ID,
coalesce(nullif(c.lastname,'')+', '+nullif(c.firstname,'')
         ,nullif(c.lastname+c.firstname,'')
         ,c.Company) AS Name
FROM @tab as c
ORDER BY LastName;
0
 
LVL 18

Expert Comment

by:deighton
ID: 37826569
SELECT c.ID, CASE WHEN LEN(coalesce (c.LastName + ', ' + c.FirstName, c.Company)) > 0 THEN coalesce (c.LastName + ', ' + c.FirstName, c.Company) ELSE c.Company END  AS Name
FROM Customer as c
ORDER BY LastName
0
 

Author Closing Comment

by:obb-taurus
ID: 37829577
Thanks dqmq.
Also, I don't need to worry if first or last name is blank as the application doesn't allow only a first or last name to be entered, both are required.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question