LNV
LNV

Reputation: 41

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.

Upvotes: 0

Views: 541

Answers (2)

tty0
tty0

Reputation: 56

Select StartTime, DATEADD(YEAR,3, StartTime) as NewStartTime FROM MYTABLE;

Upvotes: 3

Ísis Santos Costa
Ísis Santos Costa

Reputation: 391

would go yet with DATEADD

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

SQL Fiddle

Upvotes: 5

Related Questions