Solved

SQL Script for time in time format

Posted on 2014-12-17
11
97 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
11 Comments
 
LVL 51

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 51

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
 
LVL 65

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 46

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 51

Accepted Solution

by:
HainKurt earned 500 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 51

Expert Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now