Solved

SQL Server query concat question

Posted on 2013-11-18
3
276 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
3 Comments
 
LVL 65

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 125 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

777 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