Reputation: 55
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())
Upvotes: 1
Views: 1622
Reputation: 238176
I wrote a loop to check when datediff
returns an error:
; with Dates as
(
select cast('2012-01-01' as date) as dt
union all
select dateadd(day, 1, dt)
from Dates
)
select dt
, DATEDIFF(s,'1969-01-01',dt)
from Dates
option (maxrecursion 0)
The number of seconds since 1969-01-01
and 2037-01-20
is the first that is too large.
So it looks like the maximum number returned by DateDiff
is 2^31
or 2147483647
.
You can avoid the error by limiting the query's date range, like:
WHERE sd.CalendarDate >= DATEADD(day,-1,dbo.DateToday())
and sd.CalendarDate < '2037-01-20'
Upvotes: 0