Solved

SQL concatinate

Posted on 2011-03-24
9
399 Views
Last Modified: 2012-05-11
Hi,
I am trying to create a query that concatenates the columns into a single column delimited with a pipe.  It works, but if one of the columns are null, the answer is null.  How do I make it so my query will treat null as an empty string?

I will have the same issue with date and integer.

SELECT     EmpNo, AssociationName, MemberSince, Position, AssociationName + '|' + CONVERT(varchar, MemberSince) + '|' + Position AS ProfessionalAffiliationsFull
FROM         dbo.EmpAssociations AS e
0
Comment
Question by:jackjohnson44
  • 5
  • 3
9 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 250 total points
ID: 35210871
Rather than using ...

SELECT     EmpNo, AssociationName, MemberSince, Position, AssociationName + '|' + CONVERT(varchar, MemberSince) + '|' + Position AS ProfessionalAffiliationsFull
FROM         dbo.EmpAssociations AS e

Open in new window


use ...

SELECT     EmpNo, AssociationName, MemberSince, Position, ISNULL(AssociationName, '') + '|' + ISNULL(CONVERT(varchar, MemberSince), '') + '|' + ISNULL(Position, '') AS ProfessionalAffiliationsFull
FROM         dbo.EmpAssociations AS e

Open in new window


You can read about the ISNULL function here.

Regards,

Richard.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35210908
Try this way
SELECT EmpNo, AssociationName, MemberSince, Position, 
       ISNULL(AssociationName + '|' , '')+ ISNULL(CONVERT(varchar, MemberSince) + '|', '') + ISNULL(Position, '') AS ProfessionalAffiliationsFull
FROM dbo.EmpAssociations AS e

Open in new window

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 35211021
@ewangoya

It's a pain when you post a duplicate answer isn't it? <grin>
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35211039

@RQuadling

Please look carefully again. You will see a big difference
You query will put pipes even when the column value returns null
        XXX||YYY  or IXX|YY or IIXXX

Mine will not
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 250 total points
ID: 35211068

@RQuadling

Basically this is what you are doing
ISNULL(AssociationName, '') + '|'
Even if the column is null, you are still returning '|'

Mine on the other hand is
ISNULL(AssociationName + '|' , '')

So if the column is null, I dont put in a | and its much cleaner result
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 35211081
No. Please read the question ...

How do I make it so my query will treat null as an empty string?

So NULL => ''



0
 
LVL 40

Expert Comment

by:RQuadling
ID: 35211090
If the output is for a pipe delimited file (like a CSV file), then having a column or three missing is pretty much going to screw up the app/process that uses the file.
0
 

Author Comment

by:jackjohnson44
ID: 35211113
RQuadling, thanks that is what I was looking for.

How do I do the same for dates and integers?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 35212381
cast(isnull(a_numeric_column, 0) as varchar)

cast(isnull(a_date_column, '') as varchar)



0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now