Posts

Showing posts from 2015

Dealing with Comma Separated Values

Image
Sometimes we need logic where you want to hold multiple row values in single Comma separated string. Previously we were using cursor function to achieve this functionality but now a days in SQL 2012 we can achieve this functionality in simple and faster way. Below are some tricks and function by using them you can achieve the functionality of converting multiple rows in one comma separated string and converting comma separated string into multiple rows. Converting multiple rows in one comma separated string 1. Create Temp Table: Create one temp table and insert sample record in it: 2. Query: Create table #temp(email varchar(50)) insert into #temp values ( 'a.@gamil') insert into #temp values( 'b.@gamil') insert into #temp values( 'c.@gamil') Select * from #temp 3. Write Sql  COALESCE  function for comma separated string DECLARE @Names VARCHAR(8000)   SELECT @Names = COALESCE(@Names + ',', ...