SQL Server: Add seconds to a datetime field?

2020-08-09 05:06发布

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

2条回答
Lonely孤独者°
2楼-- · 2020-08-09 05:22

You should look into DATEADD.

DATEADD (datepart , number , date)

or the full update syntax

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

查看更多
Viruses.
3楼-- · 2020-08-09 05:26
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.

查看更多
登录 后发表回答