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
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*/ );
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.
SQL Server DATEADD() Function The DATEADD() function adds a time/date interval to a date and then returns the date.
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.
You should look into DATEADD.
DATEADD (datepart , number , date)
or the full update syntax
UPDATE tbl SET YourDateField = DATEADD (ss, 2, YourDateField)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With