Starscream_2017
Programmer
Hi, I am trying to find the missing dates between a date range in one table.
I have found code on this site to do that but haven't been able to get it quite right.
The missing dates should be displayed for the table tblTRF_ProductionHistory.
Sample data for the tblTRF_ProductionHistory
2017-09-30
2017-10-02
2017-10-03
2017-10-04
2017-10-05
2017-10-06
2017-10-07
2017-10-08
2017-10-10
2017-10-11
2017-10-12
Missing dates in between are required such as 2017-10-01.
Code I am trying to refactor
Set nocount on
Create Table #t (ColD date)
DECLARE @bd date, @ed date
SET @bd = '2017-9-29'
SET @ed = '2017-10-15'
While @bd<=@ed
Begin
Insert #t (ColD) Values(@bd)
Set @bd=dateadd(day,1,@bd)
End
SELECT ph.MessageDate
FROM tblTRF_ProductionHistory ph
WHERE ph.MessageDate BETWEEN '2017-9-29' AND '2017-10-15'
AND ph.Active = 1
AND SourcePlant = 51
ORDER BY MessageDate
Select Distinct [Message] = a.ColD
From #t a Left Join tblTRF_ProductionHistory b
On a.ColD = b.MessageDate
WHERE b.MessageDate IS NULL
I have found code on this site to do that but haven't been able to get it quite right.
The missing dates should be displayed for the table tblTRF_ProductionHistory.
Sample data for the tblTRF_ProductionHistory
2017-09-30
2017-10-02
2017-10-03
2017-10-04
2017-10-05
2017-10-06
2017-10-07
2017-10-08
2017-10-10
2017-10-11
2017-10-12
Missing dates in between are required such as 2017-10-01.
Code I am trying to refactor
Set nocount on
Create Table #t (ColD date)
DECLARE @bd date, @ed date
SET @bd = '2017-9-29'
SET @ed = '2017-10-15'
While @bd<=@ed
Begin
Insert #t (ColD) Values(@bd)
Set @bd=dateadd(day,1,@bd)
End
SELECT ph.MessageDate
FROM tblTRF_ProductionHistory ph
WHERE ph.MessageDate BETWEEN '2017-9-29' AND '2017-10-15'
AND ph.Active = 1
AND SourcePlant = 51
ORDER BY MessageDate
Select Distinct [Message] = a.ColD
From #t a Left Join tblTRF_ProductionHistory b
On a.ColD = b.MessageDate
WHERE b.MessageDate IS NULL