DateDiff into Hours and have decimals - sql

I have two times periods from the same day and I am using datediff(Hour,FirstTime,SecondTime)*1.0 to get the hours difference, but it is rounding. I am expecting 4.5 but my results keep coming back as 5.0
I tried casting each time, formatting each time before datediff but the results are the same. I know I am missing a simple solution probably because I am too close to the issue.

Related

Timezone independent datetime lookup

As of Sunday we've been running into a problem with our query:
SELECT COUNT(*) / 5 AS count
FROM [dbo].[LogisticalOrderLines_shadow]
WHERE SentToBI >= DATEADD(MINUTE, 55, GETDATE())
This should return the average messages sent per minute (based on the last 5 minutes) However since Monday this query has not been returning the expected results.
We are using our local time zone (was utc+1, now is utc+2) to store the time in our database. In order to prevent having to change this every half year i would like to turn this into a query which always functions no matter the timezone the server is in. but this seems to be quite an issue since we don't store utc times...
How would I go about this if it is even possible.
PS: a very strange thing happened where just for a few hours this morning I had changed the query to use -5 instead of +55 and it actually worked, that was the only way to get the correct amounts. Now however I had to change it again to 115 (which is actually what I would expect, it being UTC +2 and such). So this was quite strange and I do not have an explanation for it.

How to convert and round seconds to hours in sql server

In our database time is stored in seconds. I need to pull the time out and convert into hours with a max of 2 decimal places.
I have the following
sum (CAST(vt.TIMEINSECONDS AS decimal(10,2))/3600) as AMOUNT_TAKEN
for most part it works, but sometimes it shows 39.9999998 instead of 40 hrs and in the reports i'm running due to the time being like that its causing issues. How can i get it to show 40
CAST the result after division
CAST(SUM(vt.TIMEINSECONDS)/3600.0 AS decimal(10,2)) as AMOUNT_TAKEN
I found the right way to the round the value for my issue.
sum (CAST(ROUND(vt.TIMEINSECONDS/60.0/60.0,2)AS decimal (6,2)))
Thank you to all that helped.

Total Hours Format in Moment.js

I'm creating a data table using DataTables.net where a column contains the cumulative running hours of an event. I'm simply adding to the hours each time, so I have for example:
40:34:30
which is 40 hours, 34 minutes, 30 seconds.
My problem is I want to order this column by hours, and I haven't been able to find anything that supports this from Moment.js. Ideally I imagine it would be something like "HHH:mm:ss", or something like that. As it stands, the column recognises the fields as strings, so 0:12:34 is appearing above anything else in descending order despite only being 12 minutes long.
You can sort HH:mm:ss by re-formating it to seconds before sorting.
moment.duration('40:34:30').asSeconds;
gives you 146070. Then simply use seconds in your sorting script.
here is the solution: jsFiddle
And if you really need just the hour part; use Math.floor: jsFiddle

weird intervals in postgresql

I am working with postgresql and when i want to calcalute the average of some intervals between
two timestamps. The intervals are correct but I get something like "235 days 29:28:02.642857"
I find it strange that it says 235 days and 29 hours instead of 236 days and 5 hours.
As noted by #pozs, you can use justify_hours(interval) to get them converted properly. If you want to convert the days to months, you may use justify_days(interval). If you want to do both at once, do justify_interval(interval)

What's the proper way of rounding NSDate on iOS?

I need to get the current date, but ignoring minutes and seconds, and then calculate an interval.
For example say that it's 7:30am right now, and I need to see how many hours it is until 5pm the next day, not counting the minutes, which means 7:30am should equal to 7am in this scenario, and the result should be 22 hours.
This could be easily done with
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
assuming that I'm able to create both ends of the interval.
The only option I thought of is using NSDateFormatter to output the current date into a string without the minutes, and then parse it back. But that doesn't seem like a very elegant and efficient solution.
What I basically need to do is trim a NSDate to a specific precision, such as days, and trim the rest (7:50 to 7:00, etc.).
Or is there any simple way I can do this the smarter way, where 7:50 gets roudned to 8:00 instead of 7:00?
I need to do this on iOS 5.
The calculations can be done with NSCalendar, instead of parsing you can use NSDateComponents.
When you take the hours and minutes from NSDateComponents, you can easily apply your rounding and convert the components back to a date.