Link to home
Start Free TrialLog in
Avatar of sbornstein2
sbornstein2

asked on

SQL Server query concat question

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.
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sbornstein2
sbornstein2

ASKER

Thanks I needed all 4 fields to be checking for null.