I would like to add a time to another time.
For instance:
01:52:00.0000000 00:59:00.0000000 = 02:51:00.0000000
I know I can't do this directly with a " ".
I've tried dateadd but I can't seem to get that to work for me either.
Please help.
CodePudding user response:
You can CAST
your both times to entire dates. This allows to use DATEADD
:
SELECT CAST(DATEADD(MINUTE,
DATEPART(MINUTE, CAST('00:59:00' AS DATETIME)),
CAST('01:52:00' AS DATETIME)) AS TIME)
This ugly casting could and should be avoided by just generating whole dates instead of the time only. You could just add the current day.
SELECT CAST(DATEADD(MINUTE, DATEPART(MINUTE, '2022-11-23 00:59:00'),
'2022-11-23 01:52:00') AS TIME)
Here you need to pay attention to do not confuse day with month and run into a conversion error.
Please note I do not recommend to do such "calculations" at all in SQL. This could be much better done in your application.
You have written "I have generated these two times"...I don't know where/how you did this, but this generation should better also do this calculation rather than using SQL for that.