1

I'm using SQL Server 2014. The last line causes causes an overflow error.

Why?

  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))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3020047
  • 868
  • 1
  • 15
  • 45
  • Some simple debugging should reveal the issue. You are trying to set the year to 0 which is not valid. What are you trying to do here? – Sean Lange Sep 16 '16 at 20:50
  • Can you explain what it is that you are trying to achieve with the code? There may be an easier way – Mike Sep 16 '16 at 20:51

2 Answers2

0

You can just replace the year like so:

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

But since you set the @ModifiedStartDateTime = @PreviousYearDateTime which has the year 2015... and you are changing the year to 2015... you aren't doing anything. Try using something different than 2015 for you @YearsYear

S3S
  • 24,809
  • 5
  • 26
  • 45
-1

SQL Datetime

Date range January 1, 1753, through December 31, 9999

When you substract 2015 you are on year 0

There is DateTime2

range: 0001-01-01 through 9999-12-31.
January 1,1 CE through December 31, 9999 CE

how to solve 1753 year issue in sqlserver?

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

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118