题:一天24小时不同时间段,电的单价不同,即电价高平谷:
高峰:9:00-12:00、19:00-22:00 电单价 :1.5元每度电
平峰:8:00-9:00、12:00-19:00、22:00-24:00 电单价 :1.0元每度电
谷峰:0:00-08:00 电单价 :0.8元每度电
程序每办个小时执行一次存储过程,存储过程判断当前时间属于电价高平谷的sql如下:
declare @currentDateTime datetime; declare @currentHour float; declare @currentMinute float; declare @ElectricityUnitPrice numeric(18, 2); --电单价 declare @Clock0 datetime; declare @Clock8 datetime; declare @Clock9 datetime; declare @Clock12 datetime; declare @Clock19 datetime; declare @Clock22 datetime; declare @Clock24 datetime; set @currentDateTime=GETDATE(); set @currentHour=DATEPART(hour,@currentDateTime) --获取当前时间小时部分 set @currentMinute=DATEPART(MINUTE,@currentDateTime) set @Clock0=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 00:00:000‘); set @Clock8=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 8:00:000‘); set @Clock9=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 9:00:000‘); set @Clock12=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 12:00:000‘); set @Clock19=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 19:00:000‘); set @Clock22=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 22:00:000‘); set @Clock24=Convert(datetime,convert(varchar(10),@currentDateTime,120)+‘ 23:59:000‘); if (@currentHour=0) and (@currentMinute=0) --小时、分钟都为零,即为24点,平峰结束时刻 begin set @ElectricityUnitPrice=1.0; --平峰电价 end else --不是24点 begin if(@currentHour!=0) and (@currentMinute=0) --xxx点整 begin set @currentDateTime=dateadd(minute,-30,@currentDateTime); --临界点的区间等于前半个小时所在的区间 end if (@Clock0<@currentHour and @currentHour<@Clock8) begin set @ElectricityUnitPrice=0.8; --谷峰电价 end else if(@Clock8<@currentHour and @currentHour<@Clock9) or (@Clock12<@currentHour and @currentHour<@Clock19) or (@Clock22<@currentHour and @currentHour<@Clock24) begin set @ElectricityUnitPrice=1; --平峰电价 end else if(@Clock9<@currentHour and @currentHour<@Clock12) or(@Clock19<@currentHour and @currentHour<@Clock22) begin set @ElectricityUnitPrice=1.5; --高峰电价 end else begin --脚本无错的话,不会执行到这一步 set @ElectricityUnitPrice=-1; end end
时间: 2024-10-07 19:01:51