Skip to content
Advertisement

Replacing multiple commas with single comma in MS SQL

How do I replace consecutive commas in a column with single comma in MS SQL?

For example, I have data like

 a,,,,b,,,c,,,,,,
 d,e,,,f,,,,,,g,,  

I want this to be processed to following format:

 a,b,c,
 d,e,f,g,

The suggested duplicate, Use SQL to Replace Multiple Commas in a String with a Single Comma, is for Oracle. This is a question about SQL Server.

Advertisement

Answer

This is what i did.

select replace(replace(replace(‘a,,,b,,,c,d,e,,,,f’,’,’,'<>’),’><‘,”),'<>’,’,’)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement