Dan, why do you say adding times does not make any sense? The times given are not really 'times', they are actually durations.
That wasn't clear in your original post.
Convert both formatted strings to the most precise conceivable unit (seconds, milliseconds, nanoseconds, whatever). Add the two numbers together. Format the answer.
Better yet, if these durations are calculated with datediff, you should already have the two numbers. Add them together. Then format whatever you have to format.
Marked as answer by Chen Yu - MSFT on Nov 13, 2012 07:31 AM
smcirish
Member
732 Points
424 Posts
How to add two time fields?
Nov 06, 2012 08:36 PM|LINK
what is the syntax to add two time fields, with output in HH:MM format?
where time1 = 14:33:58
time2 = 4:30
clevesteve
Participant
1405 Points
406 Posts
Re: How to add two time fields?
Nov 06, 2012 10:06 PM|LINK
SELECT FORMAT(timecol,'HH:mm:ff') time1, FORMAT(timecol,'h:mm') FROM MYTABLE
spapim
Contributor
2446 Points
368 Posts
Re: How to add two time fields?
Nov 06, 2012 10:22 PM|LINK
You can also try:
select CONVERT(char(5), Time1, 14) as Time1, CONVERT(char(5), Time2, 14) as Time2, -- ...Hope this helps.
www.imobiliariasemsuzano.com.br
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: How to add two time fields?
Nov 06, 2012 10:32 PM|LINK
Adding two time fields together doesn't make any sense. For the two times you provided, what do you think the correct answer is?
smcirish
Member
732 Points
424 Posts
Re: How to add two time fields?
Nov 07, 2012 02:03 PM|LINK
the 2nd time is in HH:MM
Dan, why do you say adding times does not make any sense? The times given are not really 'times', they are actually durations.
clevesteve
Participant
1405 Points
406 Posts
Re: How to add two time fields?
Nov 07, 2012 02:21 PM|LINK
oh... I thought you were saying you wanted to add two time field to your query, as in return two formats of the same time!
EDIT use dateadd:
http://msdn.microsoft.com/en-us/library/ms186819.aspx
spapim
Contributor
2446 Points
368 Posts
Re: How to add two time fields?
Nov 07, 2012 03:06 PM|LINK
Try something like this:
Hope this helps.
www.imobiliariasemsuzano.com.br
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: How to add two time fields?
Nov 07, 2012 03:16 PM|LINK
That wasn't clear in your original post.
Convert both formatted strings to the most precise conceivable unit (seconds, milliseconds, nanoseconds, whatever). Add the two numbers together. Format the answer.
Better yet, if these durations are calculated with datediff, you should already have the two numbers. Add them together. Then format whatever you have to format.
smcirish
Member
732 Points
424 Posts
Re: How to add two time fields?
Nov 14, 2012 03:44 PM|LINK
Thank you 'spapim'!!! DATEADD in conjunction with DATEDIFF worked,
yahoo!
: ) smc