Get comma separated values in SQL Server

13 Flares Filament.io 13 Flares ×

Many times,We want to covert rows of SQL table into comma separated string which works as value.You can get comma separated values in SQL Server using COALESCE function or without it.

I hope you have created table,otherwise in my example check Case Statement in SQL Server for to create & insert scripts for Product Table.

After creating Product table,you will get table as shown in below image.

SQL-Server-Product-Table

COALESCE function to get comma separated values in SQL

You can use below mentioned SQL query to get comma-delimited values.

Without COALESCE function to get comma-delimited string

You can also use below method which will also build comma-separated string for table rows.!

After executing any of the above script,you will get your desired comma-delimited string as shown in below image.

comma-separated-values-in-SQL-server

Many of you think which one is better?Using COALESCE function or without.

When we have NULL values in our rows & We do not use COALESCE function then it will return NULL Result.So use of COALESCE function is a good idea.

In next tutorial,I will write about “How to insert comma separated values in different rows?”

Till then check previous post Update multiple rows in SQL Server with different values & if you have any other ideas or scripts to get comma separated string,don’t forget to share with readers and with me by commenting below.

4 thoughts on “Get comma separated values in SQL Server

  1. Good work!!!

  2. Hi
    Same thing I wanted to do using function..because in select statement I wanted to pass the values and get the o/p from function..
    I have stored the id’s in column in a table ‘1,3,5’ like this and in select statement I wanted get the description as per the id ..

    hope above will give idea abt my doubt..

    Thanks
    Santosh

  3. Good work,,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">