简单的SQl时间序列生成,每次时间间隔10分钟。


create table #timeseries(Times datetime not null)
go
declare @firstdate datetime , @lastdate datetime
declare @MI int
begin
set @firstdate='8/8/2012 6:00'
set @lastdate ='8/9/2012 6:00'
set @MI=datediff(MI,@firstdate,@lastdate)
--嵌套循环
while(@MI>=0)
begin
insert into #timeseries
values(@firstdate)
SET NOCOUNT ON
set @firstdate=dateadd(MI,10,@firstdate)--每次起始时间递增10分钟。
set @MI=datediff(MI,@firstdate,@lastdate) --当@firstdate递增至与@lastdate相同时,MI为0,退出循环
SET NOCOUNT Off
end
end
select ROW_NUMBER() over(order by Times) as id, CONVERT(varchar(10),Times, 120 ) as 日期 ,Times from #timeseries order by Times asc
drop table #timeseries

相关