我正在使用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,這是無效的。你想在這裏做什麼? –
你能解釋一下你試圖用代碼實現什麼嗎?可能有一個更簡單的方法 – Mike