Solved

SQL Query - convert decimal to hours and minutes

Posted on 2011-03-22
7
2,891 Views
Last Modified: 2012-05-11
I have a query:-

select SUM(timetaken)
from dbo.ACTIONS
where who = 'roybridge' and Whe_ >= '01/03/2011'

Open in new window


Currently, the result = 367.069999999999

How can I modify the above query so that the result is shown in hours and minutes?

Thanks
0
Comment
Question by:Chris Millard
7 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35191380
Try this:

select 
select CAST(CAST(SUM(timetaken) / 24 as int) as nvarchar(10)) + 'h ' 
     + CAST(CAST(SUM(timetaken) % 24 as int) as nvarchar(10)) + 'm' as SumTimeTaken
from dbo.ACTIONS
where who = 'roybridge' and Whe_ >= '01/03/2011'

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35191390
Sorry, it included an extra 'select', which is removed below:

select CAST(CAST(SUM(timetaken) / 24 as int) as nvarchar(10)) + 'h ' 
     + CAST(CAST(SUM(timetaken) % 24 as int) as nvarchar(10)) + 'm' as SumTimeTaken
from dbo.ACTIONS
where who = 'roybridge' and Whe_ >= '01/03/2011'

Open in new window

0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 166 total points
ID: 35191396
What's the value currently - is the whole part hours, and the decimal part fractional hours? Or is the whole part minutes, and you just want to drop the fractional part? Also, what do you want the output to look like - "HH:MM", or "X hours, Y minutes"?

Assuming your number is whole minutes and you want to drop the fraction, you could do something like this:

DECLARE @number float
    SET @number = 367.069999999999

 SELECT CONVERT(VARCHAR, CONVERT(INT, @Number/60)) + ' hours, ' + CONVERT(VARCHAR, CONVERT(INT, @Number % 60)) + ' minutes'

Open in new window


If you want something different, please post some more details.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 17

Author Comment

by:Chris Millard
ID: 35191533
@wdosanjos - when I execute the SQL query, I get the following eror:-

Msg 402, Level 16, State 1, Line 1
The data types float and int are incompatible in the modulo operator.

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 167 total points
ID: 35191963
Sorry about that.

select CAST(FLOOR(SUM(timetaken) / 60) as nvarchar(10)) + 'h ' 
     + CAST(FLOOR((SUM(timetaken) / 60 - FLOOR(SUM(timetaken) / 60)) * 60) as nvarchar(10)) + 'm' as SumTimeTaken
from dbo.ACTIONS
where who = 'roybridge' and Whe_ >= '01/03/2011'

Open in new window

0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 167 total points
ID: 35191991
This will convert the duration into HH:MI:SS format. try this.
DECLARE  @dur INT 

SELECT @dur = SUM(timetaken) 
  FROM dbo.ACTIONS 
 WHERE who = 'roybridge' 
       AND Whe_ >= '01/03/2011' 

SELECT CASE 
         WHEN @dur / 3600 < 10 THEN '0' + CONVERT(VARCHAR(10),@dur / 3600) 
         ELSE CONVERT(VARCHAR(10),@dur / 3600) 
       END + ':' + CASE 
                     WHEN (@dur%3600) / 60 < 10 THEN '0' + CONVERT(VARCHAR(10),(@dur%3600) / 60)
                     ELSE CONVERT(VARCHAR(10),(@dur%3600) / 60) 
                   END + ':' + CASE 
                                 WHEN @dur%60 < 10 THEN '0' + CONVERT(VARCHAR(10),@dur%60) 
                                 ELSE CONVERT(VARCHAR(10),@dur%60) 
                               END

Open in new window

0
 
LVL 17

Author Closing Comment

by:Chris Millard
ID: 35199994
I'm spliting the points between everyone. Basically, my query was wrong. The query was using the format mm/dd/yyyy whereas I was expecting dd/mm/yyyy. When I modify the query, I get the results I was expecting.
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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

11 Experts available now in Live!

Get 1:1 Help Now