我正在使用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