2016-09-16 159 views
1

我正在使用SQL Server 2014.最后一行导致溢出错误。SQL Server错误 - 向'datetime'列添加值导致溢出

为什么?

DECLARE @CurrentDateTime datetime 
    DECLARE @PreviousYearDateTime datetime 
    DECLARE @ModifiedStartDateTime datetime 
    DECLARE @YearsYear int = 2015 

    SET @CurrentDateTime = '2016-08-29 19:13:30.840'  
    SET @PreviousYearDateTime = CONVERT(datetime, DATEADD(YEAR, -1, @CurrentDateTime)) 

    -- For the "startdate" - just use the previous "startdate". 
    SET @ModifiedStartDateTime = @PreviousYearDateTime 

    -- Set the 'year' to the year value. 
    SET @ModifiedStartDateTime = DATEADD(YEAR, @YearsYear, DATEADD(YEAR, -DATEPART(YEAR, @ModifiedStartDateTime), @ModifiedStartDateTime)) 
+0

一些简单的调试应该会揭示问题。您正试图将年份设置为0,这是无效的。你想在这里做什么? –

+0

你能解释一下你试图用代码实现什么吗?可能有一个更简单的方法 – Mike

回答

0

您只需更换像今年这样:

SET @ModifiedStartDateTime = REPLACE(@ModifiedStartDateTime,DATEPART(YEAR,@ModifiedStartDateTime),@YearsYear) 

但因为你设置的@ModifiedStartDateTime = @PreviousYearDateTime其中有2015年......而你正在改变这一年到2015年......你什么也没有做。尝试使用与2015年不同的东西@YearsYear

-1

SQL Datetime

日期范围内1753年1月1日起,到9999

12月31日,当你。减去2015年你在一年0

DateTime2

范围:0001-01-01至9999-12-31。
月1,1 CE通过12月31日,9999 CE

how to solve 1753 year issue in sqlserver?

What is the significance of 1/1/1753 in SQL Server?

+0

当我执行“select dateadd(year,0,0)”时,它会给我“1900-01-01 00:00:00.000”。为什么不是“1753-01-01”? – BhatiaAshish

+0

我猜是[** sql设计**](https://msdn.microsoft.com/en-us/library/ms186313.aspx)'日期的参数是数字0. SQL Server将解释0作为1月1日,1900.' –

+0

谢谢!以上两个链接(伟大的曾祖父)非常有用。 – BhatiaAshish

相关问题