日期:2014-05-17  浏览次数:20727 次

两列数据合并
A B 
aa 11
bb 22

合并后的数据为 aa11,bb22 A表是varchar B表是int

------解决方案--------------------
转个类型
SQL code

select wm_concat(to_char(A)||to_char(B)) c1
from t1

------解决方案--------------------
参考:
SQL code

if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (code int,bill_code nvarchar(6))
insert into [tb]
select 4010305,'001' union all
select 4010305,'005' union all
select 4010306,'007' union all
select 4010307,'009' union all
select 4010305,'003'
if object_id('dbo.f_str')is not null drop function dbo.f_str
go
CREATE FUNCTION dbo.f_str(@code int)
RETURNS varchar(8000)
AS
BEGIN
    DECLARE @r varchar(8000)
    SET @r = ''
    SELECT @r = @r + ',' + bill_code
    FROM tb
    WHERE code=@code
    RETURN STUFF(@r, 1, 1, '')
END
GO
select dbo.f_str(4010305)
/*
001,005,003

(1 個資料列受到影響)
*/