Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-14 : 04:27:36
|
hii have a column in a table and I want to turn all the values into a string with a comma and a space.e.g.Field1 Field210001 Value110002 Value210003 Value3I want a string that says 'Value1, Value2, value3'thanks in advance |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-14 : 04:43:57
|
I dnt know what visakh has been posted because I am not able to open that link as its blocked here.But i will go for it - DECLARE @str AS VARCHAR(MAX)SET @str = ''SELECT @str = @str + ', ' + field2 FROM YourTableSELECT SUBSTRING( @str, 2, LEN(@str) )Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 04:49:31
|
quote: Originally posted by vaibhavktiwari83 I dnt know what visakh has been posted because I am not able to open that link as its blocked here.But i will go for it - DECLARE @str AS VARCHAR(MAX)SET @str = ''SELECT @str = @str + ', ' + field2 FROM YourTableSELECT SUBSTRING( @str, 2, LEN(@str) )Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
or just useSELECT STUFF((SELECT Field2 + ',' FROM Table FOR XML PATH('')),1,1,'') ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-14 : 04:52:43
|
quote: Originally posted by visakh16
quote: Originally posted by vaibhavktiwari83 I dnt know what visakh has been posted because I am not able to open that link as its blocked here.But i will go for it - DECLARE @str AS VARCHAR(MAX)SET @str = ''SELECT @str = @str + ', ' + field2 FROM YourTableSELECT SUBSTRING( @str, 2, LEN(@str) )Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER
or just useSELECT STUFF((SELECT Field2 + ',' FROM Table FOR XML PATH('')),1,1,'') ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Little correction SELECT STUFF((SELECT Field2 + ',' FROM tst FOR XML PATH('')),1,0,'')and still comma is there at end of string.Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 04:54:19
|
now ok?SELECT STUFF((SELECT ',' + Field2 FROM Table FOR XML PATH('')),1,1,'') copied from your suggestion, forgot to do change------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-08-14 : 07:05:26
|
quote: Originally posted by visakh16 now ok?SELECT STUFF((SELECT ',' + Field2 FROM Table FOR XML PATH('')),1,1,'') copied from your suggestion, forgot to do change------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Its ok.Actually I could not do that, thats why I asked you to do that. Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-14 : 11:55:00
|
thanks a lot |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 12:27:51
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|