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?

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

Improve Answer Discuss the question "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?" Watch Question

First answer by Sureshbhol. Last edit by Eisaacs. Contributor trust: 46 [recommend contributor]. Question popularity: 31 [recommend question]


Research your answer:

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Databases > 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?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
Function loop in microbiology?  Fifth Normal Form in sql server?  Where you use in sql storeprocedure?  What is the cursor SQL command for displaying tno and their names for all teachers having title 'PT'?