Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Add seconds to a datetime field?

Tags:

sql

sql-server

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

like image 545
Unknown Coder Avatar asked Aug 13 '10 21:08

Unknown Coder


People also ask

How do I add hours minutes seconds to a DateTime in SQL Server?

DECLARE @dt DATETIME = '2021-12-31 00:00:00.000' SELECT DATETIMEFROMPARTS( DATEPART(YEAR, @dt), DATEPART(MONTH, @dt), DATEPART(DAY, @dt), 23, /* hour */ 59, /* minute */ 59, /* second */ 0 /* fractional seconds*/ );

How do I add 15 minutes to time in SQL?

Simply use dateadd function to add your minutes in integer against '0:00'. Then cast back to time. Here, 84 is the integer minute I want to be expressed in "time" type.

How can add 30 minutes to current time in SQL Server?

SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.


2 Answers

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.

like image 144
Matthew Jones Avatar answered Sep 24 '22 08:09

Matthew Jones


You should look into DATEADD.

DATEADD (datepart , number , date)

or the full update syntax

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

like image 30
Nate Avatar answered Sep 22 '22 08:09

Nate