?
Solved

SQL Server query concat question

Posted on 2013-11-18
3
Medium Priority
?
295 Views
Last Modified: 2013-11-21
Hello all,

If I have 4 fields that are CustomerGroup1, CustomerGroup2, CustomerGroup3, CustomerGroup4.   I want to concat one string with a comma separator.   How do I do this so I don't have an extra comma at the end if for example 3 and 4 is null.
0
Comment
Question by:sbornstein2
[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
3 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39657709
Give this a whirl..
SELECT 
   CustomerGroup1 + ', ' + 
   CustomerGroup2 + ', ' + 
   CASE WHEN COALESCE(CustomerGroup3, '') <> '' THEN CustomerGroup3 + ', ' END + 
   CASE WHEN COALESCE(CustomerGroup4, '') <> '' THEN CustomerGroup4 END
FROM YourTable

Open in new window

0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39657716
SELECT ISNULL(CustomerGroup1, '') + ISNULL(', ' + CustomerGroup2, '') + ISNULL(', ' + CustomerGroup3, '') + ISNULL(', ' + CustomerGroup4, '')
FROM myTable
0
 

Author Closing Comment

by:sbornstein2
ID: 39666948
Thanks I needed all 4 fields to be checking for null.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

762 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