Using SQL Server 2005
Table1
ID Intime Outtime
001 00.21.00 00.48.00
002 08.23.00 13.45.00
003 00.34.00 00.18.00
I need to display the time time like 30 minutes or 1 Hours, it should display a roundoff time
Expected Output
ID Intime Outtime
001 00.30.00 01.00.00
002 08.30.00 14.00.00
003 01.00.00 00.30.00
How to make a query for the roundoff time.
Take a look at the DATEDIFF, DATEADD and DATEPART. You should be able to do what you want with that.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
http://msdn.microsoft.com/en-us/library/ms186819.aspx
http://msdn.microsoft.com/en-us/library/ms174420.aspx
Here is kind of a step-by-step routine. I'm sure you can do something shorter and even more efficient. It would also simplify a lot if you used a
datetime
data type instead of a string.You can round the current date to 30 minutes like:
Explanation: this takes the number of minutes since the 0-date:
Then it rounds that to a multiple of 30 by dividing and multiplying by 30:
The result is added back to the 0-date to find the last 30 minute block
This can be adjusted easily for 60 minutes. :)
By checking the range
or by using the classical trick equivalent to Int(x+0.5)..
IF you want to ROUNDUP instead
(you have a value going from 00.34.00 to 01.00.00) Then you need this