Hi,
I'm trying to combine data from one column on multiple rows into one field in MS SQL 2000.
ie,
From:
Accountno Reference
1234 ABC
1234 DEF
1234 GHI
2345 SDF
2345 AAA
into:
Accountno Reference
1234 ABC, DEF, GHI
2345 SDF, AAA
I have created a function to do this based on my google searches, however it will always return a null.
I'm not even sure if this is the best way to go about it, noting that there are thousands of such records.
Code for the function is as follows:
create function dbo.concatinstruments(@accountno varchar)
returns varchar(100)
as
begin
declare @output varchar(100)
select @output = coalesce(@output+', ','') + reference
from instrumentview
return @output
end
^ This doesn't work for some reason (I'm not a SQL expert)
however the following code (not a function) does work.,
declare @accountno varchar(100)
set @accountno = '1234'
declare @output varchar(100)
select @output = calesce(@output+', ','') + reference from instruments where accountno = @accountno
select accountno,@output from instruments where accountno = @accountno group by accountno
Am I calling the function wrong? I've never really dealt with functions before, so I'm dumb as a post on this.
Any help appreciated, Thanks.
create function dbo.concatinstruments(@acc
returns varchar(100)
as
begin
declare @output varchar(100)
select @output = coalesce(@output+', ','') + reference
from instrumentview where Accountno=@accountno
return @output
end
And call it
select Accountno,dbo.concatinstru
group by Accountno