July 20, 2011 at 8:39 pm
am using SQL Server 2005.
My query in the stored procedure retuns some thing like this.
Num Code_1 Code_2
1 1230 A12
1 1230 C13
1 1230 B14
2 1230 R115
3 1234 A12
3 1234 A13
4 1124 NULL
I need to conacatenete code_2 data by Num as below.
Num Code_1 Code_2
1 1230 A12,C13,B14
2 1230 R115
3 1234 A12,A13
4 1124 NULL
Can any one help me. It is urgent. Thank you in advance.
July 20, 2011 at 9:23 pm
Please post your DDL, Insert Statements as specified in the link below.
What is the purpose of this exercise? 😀
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
July 21, 2011 at 2:52 am
as per information given by you,
hope this will work
do let us know if you need something more
CREATE TABLE #temporary(num INT,code_1 VARCHAR(40),code_2 VARCHAR(40))
INSERT INTO #temporary (
num,
code_1,
code_2
) VALUES (
1, '1230','A12'),
(1, '1230', 'C13'),
(1, '1230' ,'B14'),
(2, '1230', 'R115'),
(3, '1234', 'A12'),
(3, '1234', 'A13'),
(4, '1124', NULL)
SELECT *FROM #temporary
SELECT DISTINCT num,code_1,
(STUFF((SELECT ', ' + CONVERT(NVARCHAR(1000),t.Code_2)
FROM #temporary t
WHERE t.num=tt.num
FOR XML PATH(''),
TYPE).value('.','varchar(max)'),1,1,''))
FROM #temporary tt
July 21, 2011 at 2:53 am
sry for code alinement's.
July 21, 2011 at 4:57 pm
Thank you... Thank you so much
July 21, 2011 at 5:25 pm
Post removed.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
July 22, 2011 at 5:43 am
:Wow:
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply