Please help me on this, I have been working to do conditions to get the time within the 24 hours only.
请帮我这个,我一直在努力做条件,只在24小时内得到时间。
So I have something like this. Can anyone help me or tell me if the conditions is correct.
所以我有这样的事情。任何人都可以帮助我或告诉我条件是否正确。
my @DisplayStartTime = 11:00PM
我的@DisplayStartTime =晚上11:00
Basically my @nextStartTime = 11:00PM
基本上我的@nextStartTime =晚上11点
OPEN cPhosLineTimeSlot
FETCH NEXT FROM cPhosLineTimeSlot
INTO @Parameter, @DisplayStartTime, @DisplayEndTime, @CodeEndTime, @Frequency, @Tolerance
WHILE @@FETCH_STATUS = 0
BEGIN
Declare @nextStartTime nvarchar(30)
set @nextStartTime = @DisplayStartTime
--insert into #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 INTO #ActualTimeSlot
SELECT @Parameter, @DisplayStartTime,
@nextStartTime, @CodeEndTime, @Frequency, @Tolerance
SET @intFlag +=1
END
SET @intFlag = 1
FETCH NEXT FROM cPhosLineTimeSlot
INTO @Parameter, @DisplayStartTime, @DisplayEndTime, @CodeEndTime, @Frequency, @Tolerance
END
CLOSE cPhosLineTimeSlot
DEALLOCATE cPhosLineTimeSlot
This is the sample:
这是样本:
1 个解决方案
#1
1
If you want to generate all DATETIME
values within time period [@StartDate, @EndDate]
then you could use a "tally" table:
如果要在时间段[@StartDate,@ EndDate]中生成所有DATETIME值,则可以使用“tally”表:
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
-- It generates all values from 0 to 9999
INSERT dbo.Numbers DEFAULT VALUES
GO 10000 -- You can insert more numbers if diff. between those two date/time values is greather than 13 days (I used a frecv. of 2 minutes to computes this maximum limit)
and then a simple SELECT
statement
然后是一个简单的SELECT语句
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) AS RangeStart
/*,
CASE
WHEN DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate) > @EndDate
THEN @EndDate
ELSE DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate)
END AS RangeEnd
*/
FROM dbo.Numbers n
WHERE 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
*/
#1
1
If you want to generate all DATETIME
values within time period [@StartDate, @EndDate]
then you could use a "tally" table:
如果要在时间段[@StartDate,@ EndDate]中生成所有DATETIME值,则可以使用“tally”表:
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
-- It generates all values from 0 to 9999
INSERT dbo.Numbers DEFAULT VALUES
GO 10000 -- You can insert more numbers if diff. between those two date/time values is greather than 13 days (I used a frecv. of 2 minutes to computes this maximum limit)
and then a simple SELECT
statement
然后是一个简单的SELECT语句
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) AS RangeStart
/*,
CASE
WHEN DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate) > @EndDate
THEN @EndDate
ELSE DATEADD(MINUTE, (n.Num + 1)*@Frequency, @StartDate)
END AS RangeEnd
*/
FROM dbo.Numbers n
WHERE 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
*/