Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing like this..!!!

DECLARE @minutes AS INT

SET @minutes = 1.80*100

SELECT @minutes / 60 + (@minutes % 60) / 100.0 AS Hours

it gives me 3.000 but the result should be 2.20 hrs

how can i acheive that..?

pls help me out
Posted

Integers. Interesting little devils aren't they...
What is 1.8 * 100?
Answer: 180
What is 180 / 60?
Answer: 3
What is 180 % 60?
Answer: 0

So first off the answer is 3, not 2-and-a-bit at all.

Secondly, What do you get when you integer divide a number less than 60 (as any number % 60 will always be) by 100?
Answer: 0, because integer arithmetic throws away fraction parts of numbers by definition...

So, probably, you want to consider working in floating point numbers throughout...
 
Share this answer
 
Comments
tpkpradeep 22-Feb-14 13:22pm    
Then I confess I don't how this make to work.. Pls suggest me alternate method..
VB
DECLARE @minutes INT
DECLARE @hours INT
DECLARE @time decimal(5,2) = 1.80

SET @hours = floor(@time)
SET @minutes = (@time - @hours) * 100
SET @minutes = @minutes + (@hours * 60)

SELECT  @minutes / 60 + (@minutes % 60) / 100.0 AS Hours


How about this.......!!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900