Minutes(Integer) conversion to Hours(Float) in HH.MM format in SQL Server 2008 - sql

How to convert Minutes (integer) values in to Hours float (HH.MM) in SQL Server 2008.
for example 398 Minutes get converted into 6.38 Hours, 419 Minutes get converted into 6.59 hours etc.

Please try:
SELECT 398 / 60 + (398 % 60) / 100.0
SELECT 419 / 60 + (419 % 60) / 100.0
specific
SELECT CONVERT(NUMERIC(18, 2), 398 / 60 + (398 % 60) / 100.0)
SELECT CONVERT(NUMERIC(18, 2), 419 / 60 + (419 % 60) / 100.0)

The above mentioned method fails when a decimal number is given though i'm not sure whether your application needs a decimal value.
SELECT CONVERT(NUMERIC(18, 2), 90.90 / 60 + (90.90 % 60) / 100.0)
1.82
try
select dateadd(minute,398,0)

Related

SQL Converting Minutes to Hour Increments

I am trying to alter a table within a stored procedure that will convert the minutes (i.e. 15) to hour increments (i.e. 0.25). Any help will be appreciated!
Can you try this?
Declare #minutes int = 15
SELECT #minutes / 60 + (#minutes % 60) / 60.0

Calculate duration from Times stored as Char(4)

I am trying to calculate the duration of a service where the times in the table are stored as Char(4) HHMM. For example, the start time is 1402, which would be 14:02, and the end time is 1536, which would be 15:36.
I need it to return a duration of 94 minutes. When I tried to just convert to numeric and subtract I get 134. I have tried to convert to time, but every example I have tried gives me a conversion type message.
Just convert the value to a time, and then use DATEDIFF:
SELECT DATEDIFF(MINUTE, CONVERT(time,STUFF(StartTime,3,0,':')),CONVERT(time,STUFF(EndTime,3,0,':')))
FROM dbo.YourTable;
You can use string functions and arithmetics:
select t.*,
(left(starttime, 2) * 60 + right(starttime, 2) - left(endtime, 2) * 60 - left(endtime)) / 60 minutes_diff
from mytable t
This converts the strings to a number of seconds, substracts the values, then converts back to minutes.
You can convert the difference to minutes and then add that back to a zero time:
select convert(time,
dateadd(minute,
(((et / 100) * 60) + (et % 60)) - (((st / 100) * 60) + (st % 60)),
0)
)
from (values (1402, 1536)) v(st, et);
Here is a db<>fiddle.
Thank you everyone for the answers, but I found my answer!
DateDiff(Minute, try_cast(substring(c.start_time, 1, 2) + ':' + substring(start_time, 3, 2) as time),
try_cast(substring(stop_time, 1, 2) + ':' + substring(c.stop_time, 3, 2) as time))
Task: given two parametes as char(4)
1402, 1536 in format HHMM
Return duration in minutes: 15*60+36 - 14*60+02 => 94
Solution:
SELECT (CAST(SUBSTRING('1536',1,2) AS int)*60 + CAST(SUBSTRING('1536',3,2) AS int))
- (CAST(SUBSTRING('1402',1,2) AS int)*60 + CAST(SUBSTRING('1402',3,2) AS int));

ORACLE-SQL : how to calculate 2 time in number type?

I kept my Time data as number type (NUMBER(4,2)) and I want to calculate the column like below
2.15 (2:15 am.) - 1.45 (1:45 am)
***result***
0.30 (a half hour)
Please kindly explain me the method to calculate.
Try this one I hove it will work for u
select to_char(to_date(((to_date(to_char(09.15),'hh24.mi')-to_date(to_char(01.45),'hh24.mi'))*24*60*60),'sssss'),'hh24:mi') time from dual;
Try this code:
select (trunc(2.15)* 0.6 + (2.15 - trunc(2.15))) - (trunc(1.45)* 0.6 + (1.45 - trunc(1.45)))
as result
from YOUR_TABLE
Result: 0,30
Assuming you can get them into separate columns:
with mins_calc as
(
select (floor(mytime1) - floor(mytime2))*60 + (mod(mytime1,1)-mod(mytime2,1)) as tot_mins
from Mytable
)
select to_char(floor(tot_mins/60))||'.'||to_char(mod(tot_mins,60)) as time_diff_char
from mins_calc
Convert to hours:
select ( trunc(t1) + (t1 - trunc(t1)) * 60) -
trunc(t2) + (t2 - trunc(t2)) * 60)
) as hours
This converts the difference to fractional hours. I would advise you to leave it like that or convert to minutes.
You can convert your two 'times' to minutes; this uses bind variables to provide both numeric values as it isn't clear where you're actually getting them from:
var time_1 number;
var time_2 number;
exec :time_1 := 2.15;
exec :time_2 := 1.45;
select 60 * trunc(:time_1) + 100 * (:time_1 - trunc(:time_1)) as minutes_1,
60 * trunc(:time_2) + 100 * (:time_2 - trunc(:time_2)) as minutes_2,
(60 * trunc(:time_1) + 100 * (:time_1 - trunc(:time_1)))
- (60 * trunc(:time_2) + 100 * (:time_2 - trunc(:time_2))) as minutes_diff
from dual;
MINUTES_1 MINUTES_2 MINUTES_DIFF
---------- ---------- ------------
135 105 30
You can then convert the difference in minutes back to a number in the (odd) format you're using by reversing the calculation; this uses a second CTE to get the difference in minutes calculated above to simplify things and avoid repeating the long terms:
with diff (minutes) as (
select (60 * trunc(:time_1) + 100 * (:time_1 - trunc(:time_1)))
- (60 * trunc(:time_2) + 100 * (:time_2 - trunc(:time_2)))
from dual
)
select minutes,
trunc(minutes/60) + mod(minutes, 60) / 100 as minutes_as_number
from diff;
MINUTES MINUTES_AS_NUMBER
---------- -----------------
30 .3
DATEDIFF (Transact-SQL)
This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate.
//You can return: second, minute, day, year. In your case is minute.
SELECT DATEDIFF(minute, '2018-08-03 02:15:00am', '2018-08-03 1:45:00am');
return value: -30
if you would like to get exact (30) converted in varchar use like that.
SELECT CONVERT(varchar, ABS(DATEDIFF(minute, '2018-08-03 02:15:00am', '2018-08-03 1:45:00am')));
First use ABS() to get the absolute number (removing the (-) minus signal) and convert to varchar using CONVERT().
source: https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017
ORACLE Version
SELECT TO_DATE('2000-01-02', 'YYYY-MM-DD') - TO_DATE('2000-01-01', 'YYYY-MM-DD') AS DateDiff FROM dual

I am running this query in postgres saying error 'operator does not exist: interval + double precision'

I am using this query
SELECT
(date_trunc('hour', b.pub_ts) - DATE '1970-01-01')* 24 * 60 * 60 +
EXTRACT(SECOND FROM b.pub_ts),
date_trunc('hour', b.pub_ts)
from tablename b
limit 10;
ERROR: operator does not exist: interval + double precision
LINE 1: ...ur', b.pub_ts) - DATE '1970-01-01')* 24 * 60 * 60 + EXTRACT...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
After #Vao said i try the output is
the actual output should be
Because then i have to apply round on the decimal but it is coming in interval.
Like this
round (((date_trunc('hour', b.pub_ts) - DATE '1970-01-01')* 24 * 60 * 60) +
concat(EXTRACT(SECOND FROM b.pub_ts),' seconds')::interval,3)
instead of
+ EXTRACT(SECOND FROM b.pub_ts),
try
+concat(EXTRACT(SECOND FROM b.pub_ts),' seconds')::interval,
The idea behind is that seconds is the only part of interval (or timestamp) that can have decimals, so you need to explicitely define you add seconds with decimals - then it should accept it
The answer to question
> round((extract(epoch from ((date_trunc('hour', b.pub_ts) -
DATE '1970-01-01')* 24 * 60 * 60)) +
EXTRACT(SECOND FROM b.pub_ts))::numeric,3)
AND also if you want day to be extracted instead of eposh
(round((extract(DAY from((date_trunc('hour', b.pub_ts)
- DATE '1970-01-01')* 24 * 60 * 60)) +
EXTRACT(SECOND FROM b.pub_ts))::numeric,3) *1000 / 1000)
https://www.postgresql.org/message-id/3FCF7C40.2040505%40klaster.net

Conversion algorithm milliseconds to minutes correct?

This algorithm is pretty popular, but in some cases i do get some conflict with TimeUnit results.
long millis = 12884983;
System.out.println(((millis / (1000 * 60)) % 60));
System.out.println(java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes(millis));
Prints:
34
214
First line is wrong:
System.out.println(((millis / (1000 * 60)) % 60));
and should be
System.out.println((millis / (1000 * 60)));
The mod operation cuts off your result. If you calculate 214 % 60 you get 34.