Solved

SQL concatinate

Posted on 2011-03-24
9
403 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
[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
  • 5
  • 3
9 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling 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:Ephraim Wangoya
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:Richard Quadling
ID: 35211021
@ewangoya

It's a pain when you post a duplicate answer isn't it? <grin>
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 32

Expert Comment

by:Ephraim Wangoya
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
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya 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:Richard Quadling
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:Richard Quadling
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:Richard Quadling
ID: 35212381
cast(isnull(a_numeric_column, 0) as varchar)

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



0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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