SQL Server: Add seconds to a datetime field?

4smxwvx5  于 2023-04-19  发布在  SQL Server
关注(0)|答案(3)|浏览(147)

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

bihw5rsg

bihw5rsg1#

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.

irtuqstp

irtuqstp2#

You should look into DATEADD .

DATEADD (datepart , number , date)

or the full update syntax

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

flseospp

flseospp3#

Updated additional answer for Google surfers, 2023. If you're getting syntax errors from the other answers.

Using MySQL version 8+.

UPDATE table_name SET some_datetime_column_name = DATE_ADD(some_datetime_column_name, INTERVAL 10 SECOND) WHERE some_other_column_name = 'something';

相关问题