您的位置:首页 > 数据库

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

2012-08-09 11:40 507 查看
简单的SQl时间序列生成,每次时间间隔10分钟。

use master
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




 

.net高级学习群II  Q群230160592 欢迎有基础的加入一起学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql table null