2016-09-16 44 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

相關問題