检查日期范围内的每个日期
Check Each Date In Date Range
在 SQL Server 2008 中,我将开始日期和结束日期传递给我的程序。我需要检查范围内的每个日期,看看它是否存在于我的有效工作日 table 中。我不知道从哪里开始,但这就是 start/end 天的设置方式
Declare @startdate date, @enddate date
Set @startdate = '01/01/2015'
Set @enddate = '04/16/2015'
现在我如何迭代此范围内的每个日期以查看它是否为 validworkday = true?我需要 运行 的支票是这样的(检查每个日期)
Select isvalidworkday
from validworkdays
where date = '01/01/2015'
Select isvalidworkday
from validworkdays
where date = '01/02/2015'
这是我多年前从 @Incidently 找到的语法(我不记得原来的 post 在哪里,但希望这足以证明这一点),我今天仍在使用。我所做的只是稍微调整他的语法以将数据插入临时 table 并添加一个游标来迭代每个单独的日期。
DECLARE @DateFrom smalldatetime, @DateTo smalldatetime, @firstdate date;
SET @DateFrom='20000101';
SET @DateTo='20081231';
-------------------------------
WITH T(date)
AS
(
SELECT @DateFrom
UNION ALL
SELECT DateAdd(day,1,T.date)
FROM T
WHERE T.date < @DateTo
)
SELECT date
INTO #AllDates
FROM T OPTION (MAXRECURSION 32767);
Declare c1 Cursor For
Select date
FROM #AllDates
Open c1
Fetch Next From c1 Into @firstdate
While @@FETCH_STATUS = 0
Begin
--Do whatever processing you need here
Fetch Next From c1 Into @firstdate
End
Close c1
Deallocate c1
代码应该只存在于一个地方,不能被重写。创建函数(一次),如 GetAllIntsBetween()、GetAllMonths()、GetAllDates() 等。然后像这样使用它们:
DECLARE @startdate date = '01/01/2015', @enddate date = '04/16/2015'
SELECT allDates.TheDate,
isnull(v.isvalidworkday, false) AS isvalidworkday
FROM dbo.GetAllDates(@startdate, @enddate) AS allDates
LEFT JOIN validworkdays AS v
ON allDates.TheDate = v.MyDate
GetAllDates() 将是:
CREATE FUNCTION dbo.GetAllDates(@Start DATETIME, @End DATETIME)
RETURNS
@AllDates TABLE
(
TheDate DATETIME
)
AS
BEGIN
IF @Start > @End
BEGIN
DECLARE @Temp DATETIME
SET @Temp = @Start
SET @Start = @End
SET @End = @Temp
END
WHILE @Start <= @End
BEGIN
INSERT INTO @AllDates
VALUES(@Start)
SET @Start = DATEADD(DAY, 1, @Start)
END
RETURN
END
(注:可以将DATETIME改成DATE)
在 SQL Server 2008 中,我将开始日期和结束日期传递给我的程序。我需要检查范围内的每个日期,看看它是否存在于我的有效工作日 table 中。我不知道从哪里开始,但这就是 start/end 天的设置方式
Declare @startdate date, @enddate date
Set @startdate = '01/01/2015'
Set @enddate = '04/16/2015'
现在我如何迭代此范围内的每个日期以查看它是否为 validworkday = true?我需要 运行 的支票是这样的(检查每个日期)
Select isvalidworkday
from validworkdays
where date = '01/01/2015'
Select isvalidworkday
from validworkdays
where date = '01/02/2015'
这是我多年前从 @Incidently 找到的语法(我不记得原来的 post 在哪里,但希望这足以证明这一点),我今天仍在使用。我所做的只是稍微调整他的语法以将数据插入临时 table 并添加一个游标来迭代每个单独的日期。
DECLARE @DateFrom smalldatetime, @DateTo smalldatetime, @firstdate date;
SET @DateFrom='20000101';
SET @DateTo='20081231';
-------------------------------
WITH T(date)
AS
(
SELECT @DateFrom
UNION ALL
SELECT DateAdd(day,1,T.date)
FROM T
WHERE T.date < @DateTo
)
SELECT date
INTO #AllDates
FROM T OPTION (MAXRECURSION 32767);
Declare c1 Cursor For
Select date
FROM #AllDates
Open c1
Fetch Next From c1 Into @firstdate
While @@FETCH_STATUS = 0
Begin
--Do whatever processing you need here
Fetch Next From c1 Into @firstdate
End
Close c1
Deallocate c1
代码应该只存在于一个地方,不能被重写。创建函数(一次),如 GetAllIntsBetween()、GetAllMonths()、GetAllDates() 等。然后像这样使用它们:
DECLARE @startdate date = '01/01/2015', @enddate date = '04/16/2015'
SELECT allDates.TheDate,
isnull(v.isvalidworkday, false) AS isvalidworkday
FROM dbo.GetAllDates(@startdate, @enddate) AS allDates
LEFT JOIN validworkdays AS v
ON allDates.TheDate = v.MyDate
GetAllDates() 将是:
CREATE FUNCTION dbo.GetAllDates(@Start DATETIME, @End DATETIME)
RETURNS
@AllDates TABLE
(
TheDate DATETIME
)
AS
BEGIN
IF @Start > @End
BEGIN
DECLARE @Temp DATETIME
SET @Temp = @Start
SET @Start = @End
SET @End = @Temp
END
WHILE @Start <= @End
BEGIN
INSERT INTO @AllDates
VALUES(@Start)
SET @Start = DATEADD(DAY, 1, @Start)
END
RETURN
END
(注:可以将DATETIME改成DATE)