Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Please help me with my SQL Problem

I had a data:

No Str
1 A
1 A
2 B
2 B

I want to output it like this (Since I'll be getting the data with No 1):
'A','A'

I want it to store the output into a variable. My problem is I don't know how to
put every data in single quote then comma.

This is my progress:

SQL
DECLARE @TMP VARCHAR(100)						
						
SET @TMP = (SELECT DISTINCT ''''+						
		 STUFF((SELECT '''' + ''+ [Str] + ''''				
		FROM TableN				
		WHERE [No] = 1
		FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
	    FROM TableN)					 
						
PRINT @TMP	


The output of the above query:
'A''A'

(Though I was able to output data with single quote but I'm not so sure with it.)

Please help me on how to put a comma on the above output.
My desired output is 'A','A'

Please help.
Thank you so much.
Posted
Updated 17-Jul-13 17:14pm
v4

Try this code

SQL
DECLARE @TMP VARCHAR(100)

SET @TMP = (SELECT DISTINCT ''''+
         STUFF((SELECT '''' + ''+ [Str] + ''+''','+''
        FROM @t
        WHERE [No] = 1
        FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
        FROM @t)
print SUBSTRING(@tmp,1,LEN(@tmp)-1)
 
Share this answer
 
Comments
berrymaria 18-Jul-13 1:07am    
thank you :)
ArunRajendra 18-Jul-13 1:17am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900