sql - How can compare between times in while loop in stored procedure? -
please me on this, have been working conditions time within 24 hours only.
so have this. can me or tell me if conditions correct.
my @displaystarttime = 11:00pm
basically @nextstarttime = 11:00pm
open cphoslinetimeslot fetch next cphoslinetimeslot @parameter, @displaystarttime, @displayendtime, @codeendtime, @frequency, @tolerance while @@fetch_status = 0 begin declare @nextstarttime nvarchar(30) set @nextstarttime = @displaystarttime --insert #actualtimeslot --select @parameter, @displaystarttime, @displayendtime, @codeendtime, @frequency, @tolerance while (convert(varchar, convert(time, @nextstarttime), 100) < dateadd(day, -1, getdate())) begin set @displaystarttime = @nextstarttime select @nextstarttime = ltrim(right(convert(nvarchar(100), dateadd(minute, @frequency, @nextstarttime)), 8)) insert #actualtimeslot select @parameter, @displaystarttime, @nextstarttime, @codeendtime, @frequency, @tolerance set @intflag +=1 end set @intflag = 1 fetch next cphoslinetimeslot @parameter, @displaystarttime, @displayendtime, @codeendtime, @frequency, @tolerance end close cphoslinetimeslot deallocate cphoslinetimeslot
this sample:
if want generate datetime
values within time period [@startdate, @enddate]
use "tally" table:
set nocount on go -- drop table dbo.numbers create table dbo.numbers ( num int identity(0,1), constraint pk_numbers_num primary key (num) ); go -- generates values 0 9999 insert dbo.numbers default values go 10000 -- can insert more numbers if diff. between 2 date/time values greather 13 days (i used frecv. of 2 minutes computes maximum limit)
and simple select
statement
declare @startdate datetime2(0), @enddate datetime2(0), @frequency tinyint select @startdate = '2015-04-17 11:00:00', @enddate = '2015-04-17 11:15:00', @frequency = 2; -- minutes select n.num, dateadd(minute, n.num*@frequency, @startdate) rangestart /*, case when dateadd(minute, (n.num + 1)*@frequency, @startdate) > @enddate @enddate else dateadd(minute, (n.num + 1)*@frequency, @startdate) end rangeend */ dbo.numbers n n.num <= datediff(minute, @startdate, @enddate) / @frequency /* num rangestart rangeend ----------- --------------------------- --------------------------- 0 2015-04-17 11:00:00 2015-04-17 11:02:00 1 2015-04-17 11:02:00 2015-04-17 11:04:00 2 2015-04-17 11:04:00 2015-04-17 11:06:00 3 2015-04-17 11:06:00 2015-04-17 11:08:00 4 2015-04-17 11:08:00 2015-04-17 11:10:00 5 2015-04-17 11:10:00 2015-04-17 11:12:00 6 2015-04-17 11:12:00 2015-04-17 11:14:00 7 2015-04-17 11:14:00 2015-04-17 11:15:00 */
Comments
Post a Comment