How to convert and round seconds to hours in sql server - sql

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.

Related

Invalid Time String Error when trying to change type of data from string to time

I am very new to data analytics and I need some help troubleshooting a SQL error I got. So, I have a column in this table which transferred over from Excel to SQL as a string type rather than a time piece of data. I want to make it into a time type so i can further analyze it.
So, I did the attached query to try and change the type of data using the CAST function. . However, it could not complete the query thanks to an outlier in the data set I have yet to clean the data and this was one of my first steps to so, but how do I remove this particular row that contains the invalid time string so the query can actually work? Or is there a better way to convert this entire column from text string to time?
BigQuery Time types adjust values outside the 24 hour boundary - 00:00:00 to 24:00:00; for example, if you subtract an hour from 00:30:00, the returned value is 23:30:00.
Based on your screenshot it looks like you are storing a duration? So 330 hours, 25 minutes and 55 seconds?
You would probably be best using timestamp, converting the hours to days and adding the remainder to your minutes and seconds.
You can then cast the resulting string to timestamp.
Edit
A much simpler solution is just cast('330:25:55' as interval) - thanks to #MatBailie

DateDiff into Hours and have decimals

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.

Need help converting Integer to Time

I looked through online solutions for this, none worked for me so I'm posting here. I have separate date and time columns, both stored as integers. I am able to convert the date column to DATE, but not for the Time column. The column value is: 52700, when using TO_TIME(TO_CHAR(OHCRTM)) I get 14:38:20 but it should be 07:27:00. I've tried various formatting (TO_TIME(TO_CHAR(OHCRTM),'HH24.MI.SS') but I get a 'cannot parse' error. Any idea how I can get the correct time?
14:38:20 is the right answer, unless you can give us the logic that would make it 07:27:00.
52700 seconds are exactly 14 hours, 38 minutes, and 20 seconds.
Another option to read 52700 would be a time without the colons, ie 05:27:00. To parse it like that the needed SQL is:
select to_time(52700::string, 'HHMISS');
I guess 05 becomes 07 after a timezone conversion then?

Time Formatting for MS Access Query

I'm trying to figure out the proper syntax to converting a total of minutes to show properly. For example: if something shows 65 minutes I want it to show 1:05.
What I have been messing with is as follows:
Format(Round(DateDiff("n",[StartDate],[DateCompleted]),2),"Short Time")
The query has totals turned on as this field is set to Expression. I'm getting strange results with the current criteria.
I'm sure there is something simple I'm missing but I haven't been having much luck.
Thanks!!!
As long as you won't exceed 24 hours, you can do it straight:
Duration: Format([DateCompleted]-[StartDate],"Short Time")

Check for minutes not evenly divided by 5

I have a time stored as a decimal(9,2) column in an sql-server 2005 database.
The time is represented like
Time timeInDecimal
1H 20Min 1.33
1H 30Min 1.50
and so on
I´m looking for an easy way to check whether the number of minutes except whole hours is not evenly divided by 5.
The value I'm hoping to find is where the time is 1H:23Min but not 1H:25MIN.
I just wan´t to compare the minute part of the time.
The way I do now is:
RIGHT(CONVERT(varchar(5),DATEADD(minute,ROUND(timeInDecimal * 60,0),0),108),1) not in ('0','5')
But it does hardly seems to be the ideal way to deal with this.
Feels like I can use the modulo operator for this, but how?
Or is there an even better way?
Hope for a quick answer.
Kind Regards
Andreas
Using the modulus operator, twice:
ROUND((timeInDecimal % 1) * 60, 0) % 5 <> 0
That will:
Get the fractional part and convert it to minutes.
Round it to the nearest minute (.33 hours -> 20 minutes, not 19.80).
Check whether that's divisible by 5.