Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Script for time in time format

Posted on 2014-12-17
11
Medium Priority
?
113 Views
Last Modified: 2014-12-22
Hello Experts Exchange
I have a table that has two columns [Tool Code] and [Total Down Time], I want to sum the time in the [Total Down Time] field grouped by the [Tool Code].  The [Total Down Time] column is in the format of 00:00:00.

When I run the following script the [Total Down Time] comes back as a decimal number but I want it to come back as a hour:minute:second format.

Select [Tool Code], Sum(DateDiff(Minute, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
Order by [Tool Code]

Open in new window


Here is want is returned;
Tool Code      Total Down Time
D250                            78
DM25                            1216
M116                            413
M133                            106
Not Set                             82

I want to see something like this;
Tool Code      Total Down Time
D250                           01:18:00

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 59

Expert Comment

by:HainKurt
ID: 40504716
declare @TimeinSecond int=1234;
SELECT RIGHT('0' + CAST(@TimeinSecond / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((@TimeinSecond / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(@TimeinSecond % 60 AS VARCHAR),2)

Open in new window


>>> 00:20:34
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 40504724
oops, yours in in minutes :) just multiply it by 60 to find second...

or find the difference in second before using the code above... all 59 seconds can make huge difference when summed up :) so be precise, get total in sec first then convert it to hh:mm:ss

if you wish you can use 00 as last part in the code above...

Sum(DateDiff(Minute, '0:00:00',[Total Down Time])) as [Total Down Time]
>>>
Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
0
 

Author Comment

by:SQLSearcher
ID: 40504733
Hello HainKurt
How do I integrate your code into my query?

Regards

SQLSearcher
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 66

Expert Comment

by:Jim Horn
ID: 40504741
>The [Total Down Time] column is in the format of 00:00:00.
Q1:  What is the data type of this column?  Date, datetime, varchar...
Q2:  Give us a sample data set to work with.

Offhand, you should be able to sum a time column using SUM(x), or even sum a date column as SUM(CAST(x as time))...
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40504765
I think this solution can work for you:
Select [Tool Code], Convert(time,dateadd(minute,Sum(DateDiff(Minute, '0:00:00',[Total Down Time])),0),108) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
Order by [Tool Code]

Open in new window

0
 
LVL 59

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 40504767
here:

with x as (
Select [Tool Code], Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Open in new window

0
 
LVL 59

Expert Comment

by:HainKurt
ID: 40504987
what happened to the my solutions? no comment? did not work?
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 40505003
the one i posted is perfectly fine:

with x as (
Select 'D250' [Tool Code], 78*60 as [Total Down Time]
union select'DM25', 1216*60 
union select 'M116', 413*60
union select 'M133', 106*60
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Tool Code	Total Down Time (sec)
D250	01:18:00
DM25	20:16:00
M116	06:53:00
M133	01:46:00

Open in new window

0
 

Author Comment

by:SQLSearcher
ID: 40508708
Hello Experts Exchange
After more work I have found that HainKurt solution is a better query and gives me a better amount of time calculation.


with x as (
Select [Tool Code], Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Open in new window


Sorry for the mistake.

Regards

SQLSearcher
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question