SQL Server: Add seconds to a datetime field?

SqlSql Server

Sql Problem Overview


This should be a softball for you SQL guys. I know I can add to an int field with something like UPDATE tblUser SET Total=(Total+2) but what is the syntax for adding seconds to a datetime field?

I'm using SQLServer 2008

Sql Solutions


Solution 1 - Sql

UPDATE tbluser SET DateField = DATEADD(ss,numOfSeconds,DateField)

Note the first parameter "ss". This shows that you are adding seconds to the date.

Check the docs for more info.

Solution 2 - Sql

You should look into DATEADD.

DATEADD (datepart , number , date)

or the full update syntax

UPDATE tbl SET YourDateField = DATEADD (ss, 2, YourDateField)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnknown CoderView Question on Stackoverflow
Solution 1 - SqlMatthew JonesView Answer on Stackoverflow
Solution 2 - SqlNateView Answer on Stackoverflow