SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CONVERT DATA COLUMN TO ROW WTIH COMMA


CONVERT DATA COLUMN TO ROW WTIH COMMA

Author
Message
anuj12paritosh
anuj12paritosh
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 162
HI ALL,

HOW TO COVERT COLUMN DATA IN TO ROW WITH COMMA SEPARATOR

DATA LIKE THAT :-
TABLE NAME : - TEST
COLUMN NAME : - COL1

ORIGINAL DATA ARE :-

COL1
1
2
3
4
5
6
7
1
4
5

REQUIRED DATA ARE :-

1,2,3,4,5,6,7,1,4,5

HOW TO DO THIS ....

THANKS IN ADVANCE....
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43358 Visits: 19859
Something like this?


DECLARE @Test table(
col1 int)
INSERT INTO @Test
SELECT 1 UNION ALL SELECT
2 UNION ALL SELECT
3 UNION ALL SELECT
4 UNION ALL SELECT
5 UNION ALL SELECT
6 UNION ALL SELECT
7 UNION ALL SELECT
1 UNION ALL SELECT
4 UNION ALL SELECT
5;


WITH CTE AS(
SELECT CAST( (SELECT CAST( col1 AS varchar(10))+ ','
FROM @Test b
FOR XML PATH('')
) AS VARCHAR(MAX)) concat_string
)
SELECT LEFT( concat_string, LEN(concat_string) - 1)
FROM CTE




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
wlee88
wlee88
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 13
This is what I do.
ex:
col1
1
2
3
4
5
6


declare @text varchar(500)

select @text = isnull(@text + ',','') + col1 from table1

select @text


Hope this help.
rVadim
rVadim
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1451 Visits: 2308
Using Luis' setup:



DECLARE @Test table(
col1 int)
INSERT INTO @Test
SELECT 1 UNION ALL SELECT
2 UNION ALL SELECT
3 UNION ALL SELECT
4 UNION ALL SELECT
5 UNION ALL SELECT
6 UNION ALL SELECT
7 UNION ALL SELECT
1 UNION ALL SELECT
4 UNION ALL SELECT
5;


DECLARE @S VARCHAR(8000);

SELECT
@S = COALESCE(@S, '') + CAST(col1 AS VARCHAR(10)) + ','
FROM @Test;

SELECT LEFT(@S, LEN(@S)-1); --LEFT is to remove comma at the end



Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3380 Visits: 1599
Slight modification in CTE:

WITH CTE AS(
SELECT CAST( (SELECT CAST( col1 AS varchar(10))+ ','
FROM @Test b
FOR XML PATH('')
) AS VARCHAR(MAX)) concat_string
)
SELECT STUFF(concat_string,1,1,'')
FROM CTE



~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


rVadim
rVadim
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1451 Visits: 2308
Lokesh Vij (8/21/2012)
Slight modification in CTE:

WITH CTE AS(
SELECT CAST( (SELECT CAST( col1 AS varchar(10))+ ','
FROM @Test b
FOR XML PATH('')
) AS VARCHAR(MAX)) concat_string
)
SELECT STUFF(concat_string,1,1,'')
FROM CTE



Result:

,2,3,4,5,6,7,1,4,5,

Lokesh Vij
Lokesh Vij
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3380 Visits: 1599
rVadim (8/22/2012)
Lokesh Vij (8/21/2012)
Slight modification in CTE:

WITH CTE AS(
SELECT CAST( (SELECT CAST( col1 AS varchar(10))+ ','
FROM @Test b
FOR XML PATH('')
) AS VARCHAR(MAX)) concat_string
)
SELECT STUFF(concat_string,1,1,'')
FROM CTE



Result:

,2,3,4,5,6,7,1,4,5,


Here is the corrected version:

WITH CTE AS(
SELECT CAST( (SELECT ',' + CAST( col1 AS varchar(10)) FROM @Test b
FOR XML PATH('')
) AS VARCHAR(MAX)) concat_string
)
SELECT STUFF(concat_string,1,1,'')
FROM CTE






~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search