I have 3 columns in a table in my database – Created, Expires and Expiration_Interval. Both Created and Expires are datetimeoffset(0) and Expiration_Interval is of data type Time(0).
Created gets populated with SYSUTCDATETIME(), when inserting a new row. Data for Expiration_Interval is provided in the input XML in hh:mm:ss format, extracted and populated. The requirement is to populate Expires column with Created+Expiration_Interval. Here is my approach:
INSERT INTO SESSIONS
SELECT
Expiration_Interval,
SYSUTCDATETIME(),
DATEADD(s,((DATEPART(hh,Timeout)*3600)+(DATEPART(mi,Timeout)*60)+ DATEPART(ss,Timeout)),SYSUTCDATETIME())
FROM input_xml -- CTE with shredded XML data
Is there an easier way to do this? This is on SQL Server 2008 R2
You can cast
Timeouttodatetimeand use+to add time toSYSUTCDATETIME().