Search code examples
sqlsql-servert-sqldatetimedateadd

Add Years to existing datetime column to create a 2nd datetime column


I have a column in a SQL Server table with 1,000 rows populated with various datetimes. It's called StartTime and its data type is datetime2(7).

I want to add a second column called NewStartTime that's 3 years out from the existing datetime.

For example, if row 1 value for StartTime is 2018-10-01 07:20:00.0000, I want the row 1 value for NewStartTime to be 2021-10-01 07:20:00.0000.

I've tried a number of the DATEADD functions (with and without LEFT) but can't seem to get it right.


Solution

  • would go yet with DATEADD

    select StartTime
      , dateadd(year, 3, StartTime) as NewStartTime 
     from LNVTable;
    

    SQL Fiddle