Showing posts with label SQL.. Show all posts
Showing posts with label SQL.. Show all posts

Monday, 21 March 2011

Convert the columns into a comma separated string in T-Sql

Convert the columns into a comma separated string in T-Sql

Input :-
test1
test2
test3
test4
Output:-           test1,test2,test3,test4
Query:-
SELECT STUFF((SELECT ', ' +convert(nvarchar, name) FROM (SELECT name FROM Locations_Alternative where LID=234) AS T FOR XML PATH('')),1,1,'') AS [Name]

Friday, 17 September 2010

Avoid duplicate entry while using insert into .... select statement

 Avoid duplicate entry while using insert into .... select  statement



       Insert into Names
       select name.Value as name, abr.Value as abr 
       from  name
       INNER JOIN   abr ON name.RowID = abr.RowID
       where NOT EXISTS (select 1
                 from Names a
                 where a.name =name.Value
                 and a.abr = abr.Value)