I have a column with below values and wonder how can I change it to number of minutes.
100 is 1 hr 00 min -> 60
200 is 2 hrs 00 min -> 120
My column is integer.
If it's always an INTEGER in the format of HHMM or HMM, then you can do this:
((MyColumn / 100) * 60) + MyColumn MOD 100
This will extract the hours and convert it to minutes, extract the minutes and then sum the two together.
SQL Fiddle (Postgres)
Related
is it possibile rounding time to nearest 5 minutes down?
Something like that:
select datetime('now')
return 2017-05-09 07:34:26
Select stuffcodesql
return 2017-05-09 05:00:00
Get current datetime as unix timestamp
strftime('%s', 'now')
Subtract the remainder of the division by 300 (300 seconds or 5 minutes)
strftime('%s', 'now') % 300
Convert to local time format
select datetime(strftime('%s', 'now') - strftime('%s', 'now') % 300, 'unixepoch', 'localtime') as [5min];
Convert the timestamp into a number of days;
multiply by 288 (the number of 5-minute intervals per day);
round down to the nearest integer;
divide by 288 again;
convert back into a string:
SELECT datetime(CAST(julianday('now') * 288 AS INTEGER) / 288.0);
I need to get he difference between end date and start date in milliseconds inside a view in oracle 11g. I can get these two dates from the database in 07-JUN-12 04.32.21.092000000 AM format. All I need is to find the diff of these kind of dates in milliseconds
SELECT ((extract(DAY FROM time2-time1)*24*60*60)+
(extract(HOUR FROM time2-time1)*60*60)+
(extract(MINUTE FROM time2-time1)*60)+
extract(SECOND FROM time2-time1)) *1000
as millisecs FROM dual;
can be done using above approach
select (DATE1 - DATE2) as days,
(DATE1 - DATE2) * 24 as hours,
(DATE1 - DATE2) * 24 * 60 as minutes,
(DATE1 - DATE2) * 24 * 60 * 60 as seconds,
(DATE1 - DATE2) * 24 * 60 * 60 * 1000 as milliseconds
from dual
EDIT - I assumed DateTime type. However, Justin Cave's question is very relevant. A Timestamp is not the same as a DateTime, so my answer won't work if you are dealing with Timestamps.
In that case, see this http://www.dba-oracle.com/t_timestamp_math_elapsed_times.htm.
I am trying to convert 24 hour Time values to minutes
From:
(TIME)
17:00:00
16:55:00
17:30:00
To:
(NUM)
1060
993
1038
Currently I am multiplying the time values by 60 17*60, 16.55*60, 17.30*60
How would I accomplish this? am I doing it right? and what am I doing wrong?
SAS time values are stored as seconds. If it's truly a 'time' value, anyway, and not a character string. Thus, you can DIVIDE by 60, rather than multiplying.
data want;
input timeval TIME9.;
minutes=timeval/60;
format minutes BEST12.;
format timeval TIME9.;
put timeval= minutes=;
datalines;
17:00:00
16:55:00
17:30:00
;;;;
run;
If it's not stored as a time value (numeric) but as a string, you need to INPUT(timeval,TIME9.) in order to do that; so
minutes = input(timeval,TIME9.)/60;
would work.
No idea what this has to do with SQL, but in general, assuming that hours is 0-23 rather than 0-12 with an am/pm indicator, this pseudocode
( 60.0 * hours ) // convert 0-23 hours to minutes
+ minutes // minutes don't need conversion
+ ( seconds / 60.0 ) // convert 0-59 seconds to fractional minute
should give you the offset from start of day (midnight, 00:00:00) as a fractional number of minutes. If you've got an am/pm indicator, you need to factor that in, something like this:
( 60.0 * ( hours + ( isPM ? 12 : 0 ) ) ) // convert 0-23 hours to minutes
+ minutes // minutes don't need conversion
+ ( seconds / 60.0 ) // convert 0-59 seconds to fractional minutes
I am trying to express the difference of two given dates in days, hours, and minutes (like 1 day, 6 hours, 17 minutes.) as SQLite query output. I have entryin and entryout as datetime fields in a SQLitedatabase. I tried all combinations of julianday and strftime but still running into rough weather.
I tried strftime('%d %H:%M', julianday(entryout)-julianday(entryin)). For a row the values are 2011-11-10 11:46, and 2011-11-09 09:00. but the output is 25 14:46 instead of 01 02:46.
Can some one help me with this, or point me correct logic for this? Thanks in advance.
You can try something like this:
SELECT
CAST((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) / (60 * 60 * 24) AS TEXT) || ' ' ||
CAST(((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) / (60 * 60) AS TEXT) || ':' ||
CAST((((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) % (60 * 60)) / 60 AS TEXT);
I'm trying to list the number of records per hour inserted into a database for the last 24 hours. Each row displays the records inserted that hour, as well as how many hours ago it was.
Here's my query now:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY time ASC
right now it returns:
28 23
62 23
14 20
1 4
28 3
19 1
That shows two rows from 23 hours ago, when it should only show one per hour.
I think it has something to do with using NOW() instead of getting the time at the start of the hour, which I'm unsure on how to get.
There must be a simpler way of doing this.
If you grouped by HOUR(time) then you should use HOUR(time) in your select expressions, and not time. For example:
SELECT HOUR(time), COUNT(*)
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY HOUR(time)
ORDER BY HOUR(time)
Alternatively you can group by the expression you want to return:
SELECT COUNT(*), FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
FROM `records`
WHERE time > DATE_SUB(NOW(), INTERVAL 24 HOUR)
GROUP BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
ORDER BY FLOOR( TIME_TO_SEC( TIMEDIFF( NOW(), time)) / 3600 )
In case you were wondering, it is safe to call NOW() multiple times in the same query like this. From the manual:
Functions that return the current date or time each are evaluated only once per query at the start of query execution. This means that multiple references to a function such as NOW() within a single query always produce the same result.