0

We have an hour table in our application which stores the working hours for each associate. It has hour values as follow

0.30 , 
0.30 ,
1.10

0.30 indicates 30 minutes and 1.10 indicates 1 hour 10 minutes. So when I calculate the sum of hours I got 1.7, but I need to get 1.3 (I need to convert 1.10 to 0.70).

How to achieve this?

4
  • 2
    how can you get 13 or 17 for adding 0.30, 0.30 and 1.10. Adding these up shall give 2.10. Commented Oct 9, 2015 at 8:44
  • i edited the question. adding 0.30, 0.30 and 1.10 results to 1.7 Commented Oct 9, 2015 at 8:47
  • Why aren't you using time -datatype (or datetime if the amount can exceed 24 hours)? Commented Oct 9, 2015 at 8:48
  • It seems to me that the storage format is wrong. If you cannot change it as @Michael suggests, have a look at calculatorsoup.com/calculators/time/… which explains the required calculations. I think that does not do exactly what you need, but should give you an idea/direction. Commented Oct 9, 2015 at 9:03

4 Answers 4

1

you can use some math calculation to achieve what you want ..

SELECT workHours - FLOOR(workHours) + FLOOR(workHours)*0.60

sample value:

SELECT 1.10 - FLOOR(1.10) + FLOOR(1.10)* 0.60

result: 0.70

Sign up to request clarification or add additional context in comments.

Comments

0

A simple solution would be to store the time in minutes making arithmetic simple. Then in the presentation layer convert it to the desired format.

5 Comments

Shouldn't this be a comment rather than an answer?
It is an answer though? Probably the most simplistic that anyone's going to suggest.
Well, in a way ;-) but normally an answer gives a solution/suggestion to solve the OPs problem. Such general design hints don't help normally. In most cases the design of an application is not changed for such a small reason...
Got to disagree, if an application is flawed to the point of recording time in such a useless method the application needs to be fixed.
You are right - of course - but real life differs ;-)
0

To convert 1.1 into 0.7

(int(1.1)*60+1.1%1*10)/100=.7

Comments

0

Try it like this:

DECLARE @tbl TABLE(TimeValue VARCHAR(100));
INSERT INTO @tbl VALUES
 ('0.30'),('0.30'),('1.10');

WITH Splitted AS
(
    SELECT CAST('<x>' + REPLACE(TimeValue,'.','</x><x>') + '</x>' AS XML) AS vals
    FROM @tbl
)
,TheSplittedSums AS
(
    SELECT SUM(vals.value('/x[1]','int')) AS valHour
          ,SUM(vals.value('/x[2]','int')) AS valMinute
    FROM Splitted
)
SELECT valHour*60 + valMinute 
FROM TheSplittedSums 

Delivers 130 (minutes)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.