----创建测试数据
create table tbTest(ID int, [Date] datetime)
insert tbTest
select 1, '2006-5-09' union all
select 1, '2006-5-8' union all
select 1, '2006-2-9' union all
select 2, '2006-5-8' union all
select 2, '2006-5-7' union all
select 3, '2006-5-8'
GO
----创建字符串连接函数
create function fnStringJoin(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + '/' + convert(varchar(10),[date],120)
from tbTest where id = @id
return case when @str = '' then @str else stuff(@str,1,1,'') end
end
GO
----查询
select id,dbo.fnStringJoin(id) as [date] from tbTest group by id----清除测试环境
drop table tbTest
drop function fnStringJoin/*结果
id date
1 2006-05-09/2006-05-08/2006-02-09
2 2006-05-08/2006-05-07
3 2006-05-08
*/
create table tbTest(ID int, [Date] datetime)
insert tbTest
select 1, '2006-5-09' union all
select 1, '2006-5-8' union all
select 1, '2006-2-9' union all
select 2, '2006-5-8' union all
select 2, '2006-5-7' union all
select 3, '2006-5-8'
GO
----创建字符串连接函数
create function fnStringJoin(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + '/' + convert(varchar(10),[date],120)
from tbTest where id = @id
return case when @str = '' then @str else stuff(@str,1,1,'') end
end
GO
----查询
select id,dbo.fnStringJoin(id) as [date] from tbTest group by id----清除测试环境
drop table tbTest
drop function fnStringJoin/*结果
id date
1 2006-05-09/2006-05-08/2006-02-09
2 2006-05-08/2006-05-07
3 2006-05-08
*/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货