Convert time from seconds to hours, with rounding - sql

I need to convert time from seconds to hours with 2 decimal rounding
PeriodLength contains time in seconds.
(c.PeriodLength)/3600. as Time
Returns time in hours but result is like 1.250000
and I need it to be 1.25.

Using standard SQL, you can convert it to a decimal. Something like:
select cast(c.PeriodLength / (60.00*60) as decimal(6, 2));
There are also database specific solutions, but you don't specify the database.

SET value = ROUND(value,2)
Note that this is generally used for statistical purposes, rather than monetary or time. However, it should work for what you are seeking.

Related

What do I have to change in my syntax for it to work in MS SQL Server from Informix

I'm currently updating a lot of our queries to work again in our new ERP-Release, where we will be working with MS SQL Server, swapping away from our current Informix database. Many are just simple Date-Format changes but this one I am unable to translate.
It really only is the following line:
round((GETDATE() - max(l105.dataen))::interval second(9) to second::char(10)::int
/ 60 / 60, 3)
I simply can't grasp what the part starting at the colons (::) is doing or what function it is.
I hope someone maybe can identify it.
In Informix, subtracting two DATETIME values results in an INTERVAL. The <value>::<type> notation is a shorthand for CAST(<value> AS <type>). Therefore, there are three consecutive casts:
::interval second(9) to second
::char(10)
::int
By default, if you subtract two DATETIME YEAR TO SECOND values, you will get an INTERVAL DAY(n) TO SECOND value. The first cast converts that to INTERVAL SECOND(9) TO SECOND — a number of seconds; the second cast converts the result to CHAR(10) because there isn't a direct conversion from INTERVAL to numeric types; the third cast converts the string to an INT. That gives the integer number of seconds; it is divided by 60 twice (effectively, divided by 3600) to convert the seconds into hours.
The result is then rounded to 3 decimal places.
So, the overall operation calculates the number of hours between two times.
The two times are the current time and the most recent value in the l105.dataen column (the MAX expression). Presumably, there is a GROUP BY clause somewhere in the SELECT statement that this is a part of.
You will likely need to use a 'time difference' function in MS SQL Server, and maybe the function allows you to control the presentation of the result as a number of hours and fractions of an hour.
Judging from DATEDIFF function, you will need to use something like:
DATEDIFF(hh, MAX(l105.dataen), GETDATE())
However, that returns an integer value for the difference in hours. You may prefer to get the time in seconds and divide by 3600 to get a fractional value:
DATEDIFF(ss, MAX(l105.dataen), GETDATE()) / 3600.0
No database server was consulted to ensure the veracity of the suggested translation.

Calculate time difference in SQL

We have two columns in SQL. one is total_work_time & next is total_exeption_time & both column data type is varchar
total_work_time value is 07:15:00
total_exeption_time value is 01:15:00
So I need to subtract total_work_time - total_exeption_time and the result will be 06:00:00.
I have tried with concat(DATEDIFF(HOUR,total_exeption_time,total_work_time),':', DATEDIFF(MINUTE,total_exeption_time,total_work_time))
But the result is 6:360. from this, 360 is the problem, it taken total minutes. I need the result structure like 06:00:00. How to fix this issue using SQL Server.
You should be storing time values in a TIME datatype - using the correct datatype is not only a best practice but will reduce the problems you face in future.
You can convert your VARCHAR values to TIME and then use the following calculation which takes the difference in seconds (your lowest unit of interest one assumes) and creates a new TIME result.
DECLARE #total_work_time TIME = '07:15:00', #total_exeption_time TIME = '01:15:00';
SELECT CONVERT(TIME, DATEADD(SECOND, DATEDIFF(SECOND, #total_exeption_time, #total_work_time), '00:00'));

SQL how i can convert from datetime to total hours?

I do this query and the result is a datetime, i try so much variants, but nothing work it... I want the result to be displayed in the number of total hours, like (in this case): 25:01:05 because i have 2 days en this datetime, I have had results like 01:01:05 which is when it only subtracts the hours from the datetime. I would like that as well as add the hours by the number of days, do it with the months if it can be
The time datatype in SQL Server only holds up to 24 hours.
I would recommend decimal hours instead:
select datediff(second, 0, calchstrabajos) / (60.0 * 60)
Note: I switched from millisecond to second because that is usually sufficient.
If you want this in the form of HH:MM:SS, then you would need to convert to a string. I don't recommend that.

Qlikview Timestamp formatting upto microseconds?

In qlikview I can get timestamp in milliseconds, by setting timestamp format as :-
SET TimestampFormat='MM/DD/YYYY hh:mm:ss.fff';
I want to know if there is a way to get time stamp in qlikview upto microseconds.
Formula to optain microseconds from TimeField:
((frac(TimeField) * 86400000) - floor(frac(TimeField) * 86400000)) * 1000 as Micro
And I would use this formula for formatting:
Timestamp(TimeField - (Micro/86400000000)) & Num(floor(Micro), '000') as TimeStamp
As far as I can determine from the QlikView help, there is no format specifier for microseconds, only for milliseconds.
If you need to obtain the microsecond value from a time, I quickly threw the below together (it can probably be done a bit neater). Here I assume your input time field is called TimeField. We can obtain the number of milliseconds using:
=((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000
For the sake of simplicity, I will call the above formula MillisecondCount. Then, using this field, we can then calculate the number of microseconds:
=floor(((MillisecondCount)-floor(MillisecondCount))*1000)
Finally, the full formula to obtain microseconds becomes:
floor(((((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000)
-floor(((TimeField-num(date(floor(TimeField)))-
num(maketime(hour(TimeField),minute(TimeField),second(TimeField))))*24*60*60)*1000))*1000)
You can then just format this with num() and append it to your time-stamp string.

SQL Calculations with HH:MM:SS

I have a column that comes into my SQL database as a varchar(10), but looks like HH:MM:SS. The column name is runTime.
I need to convert that column to seconds and do some calculations with it and another colunm called totalItems. totalItems is an int data type. An example of one calculation would be:
totalItems/runTime
So, it appears as though I need to convert runTime to a decimal, and then use it in the calculation, but when I can't do that without first converting the time to seconds, or I get an error (due to the ":").
So, how can I possibly convert the sessionRunTime into seconds first, and then convert those seconds to a decimal data type, then do the calculation from there? One example might be 59222 totalItems and 04:15:17 for runTime.
If there's a better way to handle this, I'm certainly open to that as well. Thanks!
So long as the hours portion doesn't match or exceed 24, you can convert to a datetime and use the built in functions to work out how long the time is in seconds:
select DATEDIFF(second,'00:00:00',CONVERT(datetime,'04:15:17'))
15317
If you need to do maths with this result, and the other value is an int, then multiple one or other of the values by 1.0 to force the maths to be done as floats, if that's required.
You can cast to datetime and use datediff to convert your string to seconds.
select datediff(second, 0, cast('04:15:17' as datetime))
Result:
15317