分享SQL2000字符字段相加!
把编号,日期相同的时间部分相加。--sql2000,利用自定义函数
drop table Tab1
Create table Tab1(bm varchar(10), xm varchar(10),kqhm varchar(10),rq datetime)
go
insert into Tab1
select 'XX办','Pj',999,'2016-10-13 07:00:05' union all
select 'XX办','Pj',999,'2016-10-13 07:02:09' union all
select 'XX办','Pj',999,'2016-10-13 07:08:55' union all
select 'XX办','Pj',999,'2016-10-14 07:09:25' union all
select 'XX办','Pj',999,'2016-10-14 07:50:45' union all
select 'XX办','Pj',999,'2016-10-14 07:55:51'
Go
select * from tab1
Select distinct bm,xm,kqhm,rq=convert(varchar(10),rq,120),result=dbo.Add_Str(kqhm,convert(varchar(10),rq,120)) from Tab1
--自定义函数
Create function Add_Str(@kqhm varchar(6),@rq varchar(10))
returns varchar(1000)
as
begin
declare @RS varchar(1000)
--如果用“,”分开,这里改为 isnull(@RS+',','')
select @RS=isnull(@RS+' ','')+right(convert(varchar(16),rq,121),5) from Tab1 where kqhm=@kqhm and convert(varchar(10),rq,120)=@rq
return @RS
end
[此贴子已经被作者于2016-10-19 10:53编辑过]