Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 DateDiff

Author  Topic 

crazyme
Starting Member

11 Posts

Posted - 2006-10-28 : 06:14:57
My requirement is i need to find the hours worked by an employee on a holiday in case if he had worked more than 7.5 hours he is eligible for an PTO on a working day, if it is less than 7.5 he is eligible for a half day PTO.

When i used datediff for hours it is giving me an integer value.

select (DATEDIFF(hh,'2006-10-28 08:15:00.270', '2006-10-28 15:45:45.270')). I want the result to be of one place decimal viz, 7.5 hours. How do i go about?



Knowledge grows when shared...

Kristen
Test

22859 Posts

Posted - 2006-10-28 : 06:39:16
select (DATEDIFF(minute,'2006-10-28 08:15:00.270', '2006-10-28 15:45:45.270')) / 60.0

Kristen
Go to Top of Page

crazyme
Starting Member

11 Posts

Posted - 2006-10-28 : 07:07:08
Thanks that worked like a chum...and one clarification is.

select (DATEDIFF(minute,'2006-10-28 08:15:00.270', '2006-10-28 15:45:45.270')) / 60.0

wat difference does it make when it is...

select (DATEDIFF(minute,'2006-10-28 08:15', '2006-10-28 15:45')) / 60

We were getting 7...and not 7.5 as desired(which we got from your query).

Knowledge grows when shared...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-28 : 07:09:23
DATEDIFF(minute,'2006-10-28 08:15:00.270', '2006-10-28 15:45:45.270') will give the time different in minute
divide by 60.0 will give in hour with decimals




KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-28 : 07:59:14
If you refer to the division with 60.0 and the division with 60, the difference is that with decimal point, the result is also decimal. Without decimal point, the result is integer division. That can lead to unpredicted results such as
18 / 5 = 3
18 / 5.0 = 3.6


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-10-28 : 14:19:55
Yeah, I probably should have explained that. It would probably be better to explicitly CAST the DATEDIFF to a Decimal, rather than relying on the side effect of using a constant with an explicit decimal (60.0 in this case) as someone not familiar with the reason might easy not realise the significance when re-reading the code in 5 years time!

Kristen
Go to Top of Page
   

- Advertisement -