如何取0点开始至今的时间段集合, 间隔1小时
如题:如何取0点开始至今的时间段集合, 间隔1小时
0:00 - 1:00
1:00 - 2:00
...
23:00 - 0:00
如
select sum(F_HourEquValue) from 数据表,where f_meterCode =10488 and f_startHour >=13:00 and F_endHour <14:00
------解决方案-------------------- select sum(F_HourEquValue) from 数据表 inner join 时间表 on 数据表.f_committime between 时间表.startime and 时间表.endtime
where f_meterCode =10488
------解决方案-------------------- 引用: 额。我这样取的时间段。
create table #seTable(starTime nvarchar(40),endTime nvarchar(40))
--declare @seTable table(starTime nvarchar(40),endTime nvarchar(40))
declare @currentTime datetime
declare @starTime datetime
declare @endTime datetime
declare @beginTime datetime
set @currentTime = GETDATE() --当前时间
set @beginTime = CONVERT(varchar(10), getdate(), 120 ) --当天开始日期2014-02-11 00:00:00.000
set @starTime = DATEADD(hour,0,@currentTime)--2014-02-11 14:00:00.000
set @starTime = CONVERT(datetime, str(DATEPART(YEAR,@starTime))+'-'+str(DATEPART(MONTH,@starTime))+'-'+str(DATEPART(DAY,@starTime))+' '+str(DATEPART(HOUR,@starTime))+':00:00',120)
set @endTime = DATEADD(hour,1,@currentTime)
set @endTime = CONVERT(datetime, str(DATEPART(YEAR,@endTime))+'-'+str(DATEPART(MONTH,@endTime))+'-'+str(DATEPART(DAY,@endTime))+' '+str(DATEPART(HOUR,@endTime))+':00:00',120)
insert into #seTable select top 24 convert(varchar(19),dateadd(hh,-langid,@starTime),120) starTime,convert(varchar(19),dateadd(hh,-langid,@endTime),120) endTime from master..syslanguages order by langid
Quote: 引用:
试试这个:
select convert(varchar(10),getdate(),120) '当天日期',
convert(varchar(5),dateadd(hour,s.number,convert(varchar(10),getdate(),120)),108)+'-'+
convert(varchar(5),dateadd(hour,s.number+1,convert(varchar(10),getdate(),120)),108) '时间段'
from master..spt_values s
where s.type = 'P' and s.number <= 23
/*
当天日期 时间段
2014-02-11 00:00-01:00
2014-02-11 01:00-02:00
2014-02-11 02:00-03:00
2014-02-11 03:00-04:00
2014-02-11 04:00-05:00
2014-02-11 05:00-06:00
2014-02-11 06:00-07:00
2014-02-11 07:00-08:00
2014-02-11 08:00-09:00
2014-02-11 09:00-10:00
2014-02-11 10:00-11:00
2014-02-11 11:00-12:00
2014-02-11 12:00-13:00
2014-02-11 13:00-14:00
2014-02-11 14:00-15:00
2014-02-11 15:00-16:00
2014-02-11 16:00-17:00
2014-02-11 17:00-18:00
2014-02-11 18:00-19:00
2014-02-11 19:00-20:00
2014-02-11 20:00-21:00
2014-02-11 21:00-22:00
2014-02-11 22:00-23:00
2014-02-11 23:00-00:00
*/
哦,这个可以适当简化:
if OBJECT_ID('tempdb..#seTable') is not null
drop table #seTable
&n