The query returns no results even on the ones where it does work and I am getting the following error.
The datediff function resulted in an overflow. The number of
dateparts separating two date/time instances is too large. Try to use
datediff with a less precise datepart.
But there is nothing to overflow
The following work:
SELECT cis.SaleBK
FROM dbo.Sales cis
INNER JOIN dim.CalendarDate sd on cis.SaleDateFK = sd.CalendarDatePK
WHERE sd.CalendarDate >= DATEADD(day,-1,dbo.DateToday())
And this one:
SELECT cis.SaleBK
,DATEDIFF(s,'1969-01-01',sd.CalendarDate) as SortOrder
FROM dbo.Sales cis
INNER JOIN dim.CalendarDate sd on cis.SaleDateFK = sd.CalendarDatePK
WHERE sd.CalendarDate = DATEADD(day,-1,dbo.DateToday())
But this does not and I can’t figure out why
SELECT cis.SaleBK
,DATEDIFF(s,'1969-01-01',sd.CalendarDate) as SortOrder
FROM dbo.Sales cis
INNER JOIN dim.CalendarDate sd on cis.SaleDateFK = sd.CalendarDatePK
WHERE sd.CalendarDate >= DATEADD(day,-1,dbo.DateToday())
I wrote a loop to check when
datediffreturns an error:The number of seconds since
1969-01-01and2037-01-20is the first that is too large.So it looks like the maximum number returned by
DateDiffis2^31or2147483647.You can avoid the error by limiting the query’s date range, like: