Nov 8, 2013

How to select or get data of a column delimited by comma in sql server

Suppose we have KeyValue like this:

Id
Key
Value
1
Name
Scott
2
Age
42
3
DOJ
12/05/2011
4
IsActive
1

We want to write a sql query which will return data of column key delimited by comma or any other character like this:

CSV
Name,Age,DOJ,IsActive

Sql server query:

DECLARE @SqlQuery AS VARCHAR(MAX) = ''
SELECT @SqlQuery = @SqlQuery + [KEY] + ',' FROM KeyValue
SELECT LEFT(@SqlQuery,LEN(@SqlQuery)-1)

No comments:

Post a Comment