Click here to Skip to main content
15,868,292 members
Articles / Database Development / SQL Server
Alternative
Tip/Trick

Adding Time in HH:MM Format - Useful SQL Query

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Jun 2011CPOL 11.1K   3
Hi,Here is an alternative for your solutionSELECT CAST(DATEADD(MILLISECOND,SUM(DATEDIFF(MILLISECOND,0,CAST(TMP.TotalTime as DATETIME))),0) as TIME)FROM( SELECT CAST(timeduration AS TIME) TotalTime FROM mytable) TMP
Hi,
Here is an alternative for your solution

SQL
SELECT CAST(DATEADD(MILLISECOND,
SUM(DATEDIFF(MILLISECOND,0,
CAST(TMP.TotalTime as DATETIME))),0) as TIME)
FROM
(
    SELECT CAST(timeduration AS TIME) TotalTime FROM mytable
) TMP

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here

Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
GeneralReason for my vote of 5 I gave this one a 5 as well. The cod... Pin
cbragdon13-Jun-11 13:50
cbragdon13-Jun-11 13:50 
GeneralTime datatype is specific to SQL SERVER 2008 Pin
Shahriar Iqbal Chowdhury/Galib8-Jun-11 0:48
professionalShahriar Iqbal Chowdhury/Galib8-Jun-11 0:48 
GeneralThis is fine but not working in MS Sql 2005; "Type time is n... Pin
Jobless Creature7-Jun-11 22:21
professionalJobless Creature7-Jun-11 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.