Solved

SQL Server query concat question

Posted on 2013-11-18
3
273 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
Comment Utility
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
Comment Utility
SELECT ISNULL(CustomerGroup1, '') + ISNULL(', ' + CustomerGroup2, '') + ISNULL(', ' + CustomerGroup3, '') + ISNULL(', ' + CustomerGroup4, '')
FROM myTable
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

743 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

16 Experts available now in Live!

Get 1:1 Help Now