Solved

SQL Server query concat question

Posted on 2013-11-18
3
275 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL:  I Want "Summary"--Not "Detail" 6 22
Add '#' to end of file 2 31
SQL Server memory sizing - reallocation 16 43
Alter a column in sql 34 0
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

862 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

23 Experts available now in Live!

Get 1:1 Help Now