Solved

SQL Script for time in time format

Posted on 2014-12-17
11
111 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 56

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 56

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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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 51

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 56

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 56

Expert Comment

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

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

635 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