Get the Column data in a single row with comma separator in SQL

In this post i thought to expalin how to get one column data in a single row with comma separator. as i shown shown below a table have one column call Choice which have 4 rows.


So our task is how to bring up all these 4 rows into one single row with comma separator.
We can do this in 2 methods,
  1. Using COALESCE() function
  2. Using ISNULL() Function

Actually both are playing same role.

1. Using COALESCE() function

As shown below in the SQL Query below, COALESCE() function takes 2 parameters.
It is check whether passed field value is Null or not if it is null then place with blank whereas if the field value is not null then concatenate value with comma.

2. Using ISNULL() Function

This query also working similar to COALESCE() function.


here value 'A' repeating twice. so how to get distinct value from this?
here it is the SQL query for that,
We just write subquery to filter only distinct values and using that as a Table for Outer query.
So here only gives A,B,C.

Comments

Popular posts from this blog

SQL SERVER – TRIM() Function – UDF TRIM()

Delete duplicate records from a Table in SQL Server