SQL错误:消息8115,级别16,状态2,行1算术溢出错误将表达式转换为数据类型datetime

时间:2016-07-20 16:06:13

标签: sql sql-server-2012

尝试执行以下查询时收到以下错误:

  

Msg 8115,Level 16,State 2,Line 1算术溢出错误   将表达式转换为数据类型datetime。

WITH recursion (employeeid, schoolyear, loc1, effectivedate, d, enddate, currentrecord, Level)
AS
(
    select this.employeeid, this.schoolyear, this.loc1, this.effectivedate, this.effectivedate, this.enddate, this.currentrecord, 0 as level
    from Staging.EmployeeStaging2 as this 
    left Join Staging.EmployeeStaging2 as [last]
        on [last].enddate + 1 = this.effectivedate
    where ([last].employeeid=this.employeeid and [last].schoolyear=this.schoolyear) and [last].loc1 <> this.loc1 or [last].loc1 is NULL

    UNION ALL

    Select this.employeeid, this.schoolyear, this.loc1, this.effectivedate, last.d, this.enddate, this.currentrecord, level + 1
    from Staging.EmployeeStaging2 as this
    inner join recursion as [last]
        on [last].enddate+1 = this.effectivedate
    where ([last].employeeid=this.employeeid and [last].schoolyear=this.schoolyear) and [last].loc1 = this.loc1 and level < 3500)select employeeid, schoolyear, loc1, d as start, max(enddate) as end1, currentrecord
from recursion where schoolyear='1516'
group by employeeid, schoolyear, d, loc1, currentrecord, level 
order by employeeid, schoolyear, d

我的字段effectivedate和enddate都是datetime。我正在使用的数据示例如下。

employeeid  schoolyear  loc1    effectivedate           effectivedate           enddate                 currentrecord   level

986607200   1516        0151    2015-07-31 00:00:00.000 2015-07-31 00:00:00.000 9999-12-31 00:00:00.000 Y               0

我研究了这个问题,我理解它与从日期字段中减去1有关。起初我认为我收到此错误,因为字段是日期而不是日期时间。我改变了字段,我仍然收到这个错误。有人请帮我理解我做错了什么吗?

对不起,很长的帖子;我是新手,仍在学习提问的正确礼仪。我应该注意到我正在使用SQL Server 2012。

1 个答案:

答案 0 :(得分:0)

试试这个

使用DATEADD

顺便说一下,你使用EndDate作为最大日期:

fs.inotify.max_user_instances = 256

  

最短日期是:1900年1月1日
  最长日期是:2079年6月6日

SMALLDATETIME

  

最短日期是:1753年1月   最长日期是:9999年12月31日

DATETIME