January 15, 2009 at 4:11 am
I have a table with 10 rows. Datas of the rows are 1, then 2, then 3 ...10.
I want the output as 1,2,3,4,5 in one cell.
Please provide the SQL for it. I dont want to use cursor or temp tables.
January 15, 2009 at 4:28 am
Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Why do you want to concatenate the values? If that's for display, it's better to do it on the front end? If it's for storage, that's not a good way to store data within SQL server.
Why is there a restriction against temp tables?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 15, 2009 at 4:38 am
DECLARE @RESULT varchar(128)
SELECT @RESULT = COALESCE(@RESULT, ', ' , '') + YourColumnName
FROM YourTableName
SELECT @RESULT
If the column is not char/varchar type the you will have to use cast
-Vikas Bindra
January 15, 2009 at 4:40 am
try this code hope this will help you...
declare @strCVS VARCHAR(500)
select @strCVS = coalesce(@strCVS + ',', '') + cast(object_id as varchar(10))
from sys.objects
select @strCVS
Abhijit - http://abhijitmore.wordpress.com
January 15, 2009 at 5:05 am
HI Avijit and Vikas,
Thanks for the help. This is exactly what I need.
Thanks again.
January 15, 2009 at 5:07 am
Hi Gail,
This is actualy a question, which I have faced in an interview. I have tried with ROW_Number function later but no result.
That's why I have posted it. Its easy to do the same using cursor or temp table, but also possibel through single SQL.
I have got my answer from Vikas and Avijeet.
Thanks again for your help.
January 15, 2009 at 6:16 am
arup_kc (1/15/2009)
Hi Gail,This is actualy a question, which I have faced in an interview.
I thought as much.
Just note with the solutions above, there's no guarantee of order. It may come out in the order you want, it may not. There's a trick with FOR XML PATH that returns data the same way, can partition the comma-deliminated strings and does guarantee an order, when an ORDER BY is applied
Since it's an interview question, I'll leave you to google for it. It shouldn't be hard to find
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 15, 2009 at 6:21 am
Thanks gail, I ll try your solution also.
January 16, 2009 at 3:28 am
AS said this can be done by usning FOR XML path also....
SELECTSTUFF((
SELECTDISTINCT ', ' + cast(object_id as varchar(10))
FROMsys.objects
FOR XML PATH('')), 1, 1, '') AS 'Ids'
Abhijit - http://abhijitmore.wordpress.com
January 16, 2009 at 5:01 am
hi Arup,
you can use the following code to get comma saperated data in a cell.
declare @Column1 varchar(8000)
set @Column1=''
select @Column1=@Column1+','+convert(varchar,Column1) from Table1
select substring(@Column1,2,len(@Column1))
Thanks & Regards
Rakesh Singh
January 17, 2009 at 2:07 pm
Try this it may be simple.
Select Stuff (
SELECT ',' + CONVERT(ID AS VARCHAR(50))
FROM myIdTable
ORDER BY ID
FOR XML PATH('')
) , 1,1,'')
Regards
Vinay
January 17, 2009 at 7:31 pm
arup_kc (1/15/2009)
Hi Gail,This is actualy a question, which I have faced in an interview. I have tried with ROW_Number function later but no result.
That's why I have posted it. Its easy to do the same using cursor or temp table, but also possibel through single SQL.
I have got my answer from Vikas and Avijeet.
Thanks again for your help.
Actually, despite all of the seemingly correct answers you've been provided, you still don't have the correct answer... at least not in an interview with me. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
January 17, 2009 at 11:34 pm
GilaMonster (1/15/2009)
Just note with the solutions above, there's no guarantee of order. It may come out in the order you want, it may not. There's a trick with FOR XML PATH that returns data the same way, can partition the comma-deliminated strings and does guarantee an order, when an ORDER BY is applied
Actually, unlike UPDATE pseudocursors, SELECT pseudocursors can use an ORDER BY:declare @strCVS VARCHAR(500)
select @strCVS = coalesce(@strCVS + ',', '') + cast(object_id as varchar(10))
from sys.objects
order by object_id desc
select @strCVS
which as I understand BOL should be effective:
ORDER BY guarantees a sorted result only for the outermost SELECT statement of a query.
This being an ORDER BY on the outermost SELECT, it seems that it is guaranteed to be so sorted.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
January 18, 2009 at 2:35 am
RBarryYoung (1/17/2009)
This being an ORDER BY on the outermost SELECT, it seems that it is guaranteed to be so sorted.
Ummm. This is an odd case. I chatted with one of the QO team on this, and Itzik mentioned it in his pre-con at Pass as well as well. It's another case of 'usually works'. The concatenation (SELECT @var = @var + somecol) wasn't written to concat an entire table. It was written to do constant or variable concatenation. Usually it'll work, in odd circumstances the concatenation will be done before the order by and once in a while it doesn't work the way it's expected.
Bear in mind that the order by is the last cause applied, after the select is done.
I recall a post here (I can't remember who it was or who helped) where someone used this and when the order by was added, only one row was processed. (Were you involved in that?)
The xml path was written to do the concatenation, it's not an accident that it works that way.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 18, 2009 at 6:40 am
As you say Gail, however, this is a case where, as documented, it is supposed to work.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply