Solved

SQL Script for time in time format

Posted on 2014-12-17
11
104 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
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.

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Generate Scripts Fails 5 36
best counters for cpu high usage 3 35
New to SSRS, extremely slow running report. 8 21
SQL Function NOT ROUND 9 11
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. …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

825 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