簡單的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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章