Is there any function in sql server to return all values of a column seprated by comma you dont want to use loop or sql sever cursor?
-
-
Answer:
Sure it can be done... CREATE TABLE #T ( ID INT, [NAME] VARCHAR(50) ) INSERT #T SELECT 1, 'Row1' UNION SELECT 2, 'Row2' UNION SELECT 3, 'Row3' UNION SELECT 4, 'Row4' UNION SELECT 5, 'Row5' UNION SELECT 6, 'Row6' UNION SELECT 7, 'Row7' UNION SELECT 8, 'Row8' UNION SELECT 9, 'Row9' UNION SELECT 10, 'Row10' SELECT * FROM #T DECLARE @Values AS VARCHAR(8000) SELECT @Values = '' SELECT @Values = @Values + ISNULL([Name] + ', ', '') FROM #T IF LEN(@Values) > 0 SELECT @Values = LEFT(@VALUES, LEN(RTRIM(@VALUES)) - 1) --Remove trailing comma SELECT @Values AS CommaDelimitedResultSet DROP TABLE #T
wiki.answers.com Visit the source
Related Q & A:
- How to control the excessive use of ram by SQL Server?Best solution by Database Administrators
- Does it ever make sense to use RAM Disk to force RAM allocation for tempdb with SQL Server 2008?Best solution by Database Administrators
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- I want to avoid dynamic pivot in SQL Server.Best solution by Stack Overflow
- How to change column data's as a separate column wise format in a SQL Server?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.