在预定义日期中添加一天 SQL Server 2008 R2
Add a day to a predefined date SQL Server 2008 R2
我的问题很简单,我正在查询,我打算将两个日期插入到临时 Table。
即 2020-04-01 00:00:00 和 20-04-01 23:59:00,因此您需要的天数会增加。
我是这样做的 E.G:
DECLARE @StartDate DATETIME = '2020-04-01'
DECLARE @EndDate DATETIME = '2020-04-01'
SET @StartDate = @StartDate + '00:00:00'
SET @EndDate = @EndDate + '23:59:00'
WHILE (@StartDate <= @EndDate)
BEGIN
--Insert into a temp table
set @StartDate = DATEADD(DAY, 1, @StartDate);
set @EndDate = DATEADD(day, 1, @EndDate);
print @StartDate
print @EndDate
END;
,然而它进入了一个重复的循环,并没有停止。而且,我需要的不仅仅是尊重天数的增量。
有人可以帮我解决这个问题吗?
我想讲这个:
初始日期:2020-04-01 00:00:00.
结束日期:2020-04-01 23:59:00。
开始日期增量 2020-04-02 00:00:00
结束日期增量:2020-04-02 23:59:00。
我的查询向我抛出这个:
Apr 2 2020 12:00AM
Apr 2 2020 11:59PM
Apr 3 2020 12:00AM
Apr 3 2020 11:59PM
Apr 4 2020 12:00AM
Apr 4 2020 11:59PM
Apr 5 2020 12:00AM
Apr 5 2020 11:59PM
Apr 6 2020 12:00AM
Apr 6 2020 11:59PM
Apr 7 2020 12:00AM
Apr 7 2020 11:59PM
Apr 8 2020 12:00AM
Apr 8 2020 11:59PM
Apr 9 2020 12:00AM
Apr 9 2020 11:59PM
Apr 10 2020 12:00AM
Apr 10 2020 11:59PM
Apr 11 2020 12:00AM
Apr 11 2020 11:59PM
Apr 12 2020 12:00AM
Apr 12 2020 11:59PM
Apr 13 2020 12:00AM
Apr 13 2020 11:59PM
Apr 14 2020 12:00AM
Apr 14 2020 11:59PM
Apr 15 2020 12:00AM
Apr 15 2020 11:59PM
Apr 16 2020 12:00AM
Apr 16 2020 11:59PM
Apr 17 2020 12:00AM
Apr 17 2020 11:59PM
Apr 18 2020 12:00AM
Apr 18 2020 11:59PM
Apr 19 2020 12:00AM
Apr 19 2020 11:59PM
Apr 20 2020 12:00AM
Apr 20 2020 11:59PM
Apr 21 2020 12:00AM
Apr 21 2020 11:59PM
Apr 22 2020 12:00AM
Apr 22 2020 11:59PM
Apr 23 2020 12:00AM
Apr 23 2020 11:59PM
Apr 24 2020 12:00AM
Apr 24 2020 11:59PM
Apr 25 2020 12:00AM
Apr 25 2020 11:59PM
Apr 26 2020 12:00AM
Apr 26 2020 11:59PM
Apr 27 2020 12:00AM
Apr 27 2020 11:59PM
Apr 28 2020 12:00AM
Apr 28 2020 11:59PM
Apr 29 2020 12:00AM
Apr 29 2020 11:59PM
Apr 30 2020 12:00AM
Apr 30 2020 11:59PM
May 1 2020 12:00AM
May 1 2020 11:59PM
May 2 2020 12:00AM
May 2 2020 11:59PM
May 3 2020 12:00AM
May 3 2020 11:59PM
May 4 2020 12:00AM
May 4 2020 11:59PM
May 5 2020 12:00AM
May 5 2020 11:59PM
May 6 2020 12:00AM
May 6 2020 11:59PM
May 7 2020 12:00AM
May 7 2020 11:59PM
May 8 2020 12:00AM
May 8 2020 11:59PM
May 9 2020 12:00AM
May 9 2020 11:59PM
May 10 2020 12:00AM
May 10 2020 11:59PM
May 11 2020 12:00AM
May 11 2020 11:59PM
May 12 2020 12:00AM
May 12 2020 11:59PM
May 13 2020 12:00AM
May 13 2020 11:59PM
May 14 2020 12:00AM
May 14 2020 11:59PM
May 15 2020 12:00AM
May 15 2020 11:59PM
May 16 2020 12:00AM
May 16 2020 11:59PM
May 17 2020 12:00AM
May 17 2020 11:59PM
May 18 2020 12:00AM
May 18 2020 11:59PM
May 19 2020 12:00AM
May 19 2020 11:59PM
May 20 2020 12:00AM
May 20 2020 11:59PM
May 21 2020 12:00AM
May 21 2020 11:59PM
May 22 2020 12:00AM
May 22 2020 11:59PM
May 23 2020 12:00AM
May 23 2020 11:59PM
May 24 2020 12:00AM
May 24 2020 11:59PM
May 25 2020 12:00AM
May 25 2020 11:59PM
May 26 2020 12:00AM
May 26 2020 11:59PM
May 27 2020 12:00AM
May 27 2020 11:59PM
May 28 2020 12:00AM
May 28 2020 11:59PM
May 29 2020 12:00AM
May 29 2020 11:59PM
May 30 2020 12:00AM
May 30 2020 11:59PM
May 31 2020 12:00AM
May 31 2020 11:59PM
Jun 1 2020 12:00AM
Jun 1 2020 11:59PM
Jun 2 2020 12:00AM
Jun 2 2020 11:59PM
Jun 3 2020 12:00AM
Jun 3 2020 11:59PM
Jun 4 2020 12:00AM
Jun 4 2020 11:59PM
Jun 5 2020 12:00AM
Jun 5 2020 11:59PM
Jun 6 2020 12:00AM
Jun 6 2020 11:59PM
Jun 7 2020 12:00AM
- 有什么建议吗?
- 我的错误在哪里?
谢谢。
使用递归 CTE:
DECLARE @StartDate DATETIME = '2020-04-01';
DECLARE @EndDate DATETIME = '2020-04-01';
with dates as (
select @startdate as startdate, dateadd(minute, -1, dateadd(day, 1, @startdate)) as enddate
union all
select dateadd(day, 1, startdate), dateadd(day, 1, enddate)
from dates
where startdate < @enddate
)
select *
from dates
option (maxrecursion 0);
Here 是一个 db<>fiddle.
我的问题很简单,我正在查询,我打算将两个日期插入到临时 Table。
即 2020-04-01 00:00:00 和 20-04-01 23:59:00,因此您需要的天数会增加。
我是这样做的 E.G:
DECLARE @StartDate DATETIME = '2020-04-01'
DECLARE @EndDate DATETIME = '2020-04-01'
SET @StartDate = @StartDate + '00:00:00'
SET @EndDate = @EndDate + '23:59:00'
WHILE (@StartDate <= @EndDate)
BEGIN
--Insert into a temp table
set @StartDate = DATEADD(DAY, 1, @StartDate);
set @EndDate = DATEADD(day, 1, @EndDate);
print @StartDate
print @EndDate
END;
,然而它进入了一个重复的循环,并没有停止。而且,我需要的不仅仅是尊重天数的增量。
有人可以帮我解决这个问题吗?
我想讲这个:
初始日期:2020-04-01 00:00:00.
结束日期:2020-04-01 23:59:00。
开始日期增量 2020-04-02 00:00:00
结束日期增量:2020-04-02 23:59:00。
我的查询向我抛出这个:
Apr 2 2020 12:00AM
Apr 2 2020 11:59PM
Apr 3 2020 12:00AM
Apr 3 2020 11:59PM
Apr 4 2020 12:00AM
Apr 4 2020 11:59PM
Apr 5 2020 12:00AM
Apr 5 2020 11:59PM
Apr 6 2020 12:00AM
Apr 6 2020 11:59PM
Apr 7 2020 12:00AM
Apr 7 2020 11:59PM
Apr 8 2020 12:00AM
Apr 8 2020 11:59PM
Apr 9 2020 12:00AM
Apr 9 2020 11:59PM
Apr 10 2020 12:00AM
Apr 10 2020 11:59PM
Apr 11 2020 12:00AM
Apr 11 2020 11:59PM
Apr 12 2020 12:00AM
Apr 12 2020 11:59PM
Apr 13 2020 12:00AM
Apr 13 2020 11:59PM
Apr 14 2020 12:00AM
Apr 14 2020 11:59PM
Apr 15 2020 12:00AM
Apr 15 2020 11:59PM
Apr 16 2020 12:00AM
Apr 16 2020 11:59PM
Apr 17 2020 12:00AM
Apr 17 2020 11:59PM
Apr 18 2020 12:00AM
Apr 18 2020 11:59PM
Apr 19 2020 12:00AM
Apr 19 2020 11:59PM
Apr 20 2020 12:00AM
Apr 20 2020 11:59PM
Apr 21 2020 12:00AM
Apr 21 2020 11:59PM
Apr 22 2020 12:00AM
Apr 22 2020 11:59PM
Apr 23 2020 12:00AM
Apr 23 2020 11:59PM
Apr 24 2020 12:00AM
Apr 24 2020 11:59PM
Apr 25 2020 12:00AM
Apr 25 2020 11:59PM
Apr 26 2020 12:00AM
Apr 26 2020 11:59PM
Apr 27 2020 12:00AM
Apr 27 2020 11:59PM
Apr 28 2020 12:00AM
Apr 28 2020 11:59PM
Apr 29 2020 12:00AM
Apr 29 2020 11:59PM
Apr 30 2020 12:00AM
Apr 30 2020 11:59PM
May 1 2020 12:00AM
May 1 2020 11:59PM
May 2 2020 12:00AM
May 2 2020 11:59PM
May 3 2020 12:00AM
May 3 2020 11:59PM
May 4 2020 12:00AM
May 4 2020 11:59PM
May 5 2020 12:00AM
May 5 2020 11:59PM
May 6 2020 12:00AM
May 6 2020 11:59PM
May 7 2020 12:00AM
May 7 2020 11:59PM
May 8 2020 12:00AM
May 8 2020 11:59PM
May 9 2020 12:00AM
May 9 2020 11:59PM
May 10 2020 12:00AM
May 10 2020 11:59PM
May 11 2020 12:00AM
May 11 2020 11:59PM
May 12 2020 12:00AM
May 12 2020 11:59PM
May 13 2020 12:00AM
May 13 2020 11:59PM
May 14 2020 12:00AM
May 14 2020 11:59PM
May 15 2020 12:00AM
May 15 2020 11:59PM
May 16 2020 12:00AM
May 16 2020 11:59PM
May 17 2020 12:00AM
May 17 2020 11:59PM
May 18 2020 12:00AM
May 18 2020 11:59PM
May 19 2020 12:00AM
May 19 2020 11:59PM
May 20 2020 12:00AM
May 20 2020 11:59PM
May 21 2020 12:00AM
May 21 2020 11:59PM
May 22 2020 12:00AM
May 22 2020 11:59PM
May 23 2020 12:00AM
May 23 2020 11:59PM
May 24 2020 12:00AM
May 24 2020 11:59PM
May 25 2020 12:00AM
May 25 2020 11:59PM
May 26 2020 12:00AM
May 26 2020 11:59PM
May 27 2020 12:00AM
May 27 2020 11:59PM
May 28 2020 12:00AM
May 28 2020 11:59PM
May 29 2020 12:00AM
May 29 2020 11:59PM
May 30 2020 12:00AM
May 30 2020 11:59PM
May 31 2020 12:00AM
May 31 2020 11:59PM
Jun 1 2020 12:00AM
Jun 1 2020 11:59PM
Jun 2 2020 12:00AM
Jun 2 2020 11:59PM
Jun 3 2020 12:00AM
Jun 3 2020 11:59PM
Jun 4 2020 12:00AM
Jun 4 2020 11:59PM
Jun 5 2020 12:00AM
Jun 5 2020 11:59PM
Jun 6 2020 12:00AM
Jun 6 2020 11:59PM
Jun 7 2020 12:00AM
- 有什么建议吗?
- 我的错误在哪里?
谢谢。
使用递归 CTE:
DECLARE @StartDate DATETIME = '2020-04-01';
DECLARE @EndDate DATETIME = '2020-04-01';
with dates as (
select @startdate as startdate, dateadd(minute, -1, dateadd(day, 1, @startdate)) as enddate
union all
select dateadd(day, 1, startdate), dateadd(day, 1, enddate)
from dates
where startdate < @enddate
)
select *
from dates
option (maxrecursion 0);
Here 是一个 db<>fiddle.