I'm trying to convert a duration column which is in a Date/Time format to minutes Integer.
so for 8 Hours the data looks like this 01/01/1970 08:00:00 what can I do to convert to 480 minutes
I'm connecting to Service Now DB through a ODBC connector
Thanking you in advance
In standard SQL, you can use:
select extract(hour from duration) * 60
If you wanted to include minutes as well:
select extract(hour from duration) * 60 + extract(minute from duration)
However, date/time functions vary significantly depending on the underlying database. So, you may need to tweak the above logic.
Related
I'm attempting to get the difference of time in seconds between two timestamps on an sqlite db (running sqlite 3.12.1) I've tried several things I have found here on stack overflow but nothing seems to be working as expected.
for reference I've tried creating the datetime field as both an integer and a text field and I get the same results. Below is the query I'm running and the results set that it produces.
essentially I'm attempting to replicate the datediff() function in SQL Server
select cast(JulianDay('now') - JulianDay(last_upd_dt) * 24 * 60 * 60 As Integer) "gives me null value",
last_upd_dt as "from date",
datetime('now') as "To date this will be another field in the future",
(datetime('now') - last_upd_dt ) as "Gives me 2012 on all records"
from sqlite_db
Your date value should be stored as a TEXT datatype, in one of the formats supported by SQLite, such as YYYY-MM-DD HH:MI:SS.
Then, you can do
select (julianday('now') - julianday(last_upd_dt)) * 24 * 60 * 60 as date_diff_seconds
from mytable
The problem with your code is that you should use parentheses around the difference of the dates and then multiply by 24 * 60 * 60.
But there is a much simpler way to get the difference between 2 dates in seconds:
select strftime('%s', 'now') - strftime('%s', last_upd_dt)
from sqlite_db
The function strftime() with the '%s' modifier returns the unix timestamp (the number of seconds since '1970-01-01') of a date (provided it is formatted as YYYY-MM-DD hh:mm:ss).
I have a database that contains a series of events and their timestamp.
I find myself needing to select all events that happen between 11:00 and 11:10 and 21:00 and 21:05, for all days.
So what I would do is I extract from timestamp the hour and the minute, and:
SELECT *
WHERE (hour = 11 AND minute <= 10)
OR (hour = 21 AND minute <= 05)
However, I was wondering if there's a simpler / less verbose way to do this, such as when you query between dates:
SELECT *
WHERE date BETWEEN '2020-07-01' AND '2020-07-05'
I read here that this is doable in SQLite, I was wondering if it's possible to be done in presto as well. I've looked at the docs but couldn't find an analogue function that does what time() does in SQLite.
You could use date formatting functions, e.g. date_format, then string comparisons:
select *
from mytable
where
date_format(mydate, '%H:%i') between '11:00' and '11:09'
or date_format(mydate, '%H:%i') between '21:00' and '21:04'
Note that I substracted one minute from the upper bound, since I assume you don't want to include the last minute. between '11:00' and '11:09' gives you everything from 11:00:00 to 11:09:59.
I need to know a difference between start time and end time. Both are DATETIME fields, I tried to use "-" and DATADIFF.
I already tried using DATADIFF and simple subtraction converting the field to just time.
(to_date(Fim_Hora,'HH24:MI') - to_date(Inicio_Hora,'HH24:MI')) AS Diferenca
DATADIFF(MIN,Fim_Hora,Inicio_Hora)
I need to know the time in minutes for use as parameters.
Oracle does not have a time data type. Usually, subtraction works well enough:
select (end_time - start_time) as diff
You may need to convert to a string if you want it formatted in a particular way.
In Oracle, you can directly substract dates, it returns the difference between the dates in days. To get the difference in minutes, you can multiply the result by 24 (hours per days) and 60 (minutes per hour):
(Fim_Hora - Inicio_Hora) * 24 * 60 diff_minutes
This assumes that both Fim_Hora and Inicio_Hora are of datatype DATE.
I have a hive table that contains a column called timestamp. The timestamp is a bigint field generated from java System.currenttimemillis(). I suppose it should be in UTC. Right now I am trying to select records from 1 hour ago. I know in MySQL you can do something like:
SELECT * FROM table WHERE datetimefield >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
In hive, it seems like NOW() is missing. I did some searching and find unix_timestamp(). I should be able to get the current UTC time in milliseconds by doing a unix_timestamp()*1000.
So if i want to get records from 1 hour ago I am thinking about doing something like:
SELECT * FROM hivetable WHERE datetimefield >= (unix_timestamp()*1000-3600000);
Can someone suggest if it's the right way to approach this problem? Also what if I want to select like 1 day ago? Seems inconvenient to convert that to milliseconds. Any help or suggested readings will be highly appreciated. Thanks in advance for your help.
Yes unix_timestamp() gets you the seconds elapsed since Unix epoch. You can subtract 60*60*1000 milliseconds and compare your field to get the desired records.
For Hive 1.2.0 and higher you can use current_timestamp
select *
from hivetable
where
datetimefield >= ((unix_timestamp()*1000) - 3600000);
For 1 day,convert the milliseconds to date format and use date_sub
select *
from hivetable
where
from_unixtime(unix_timestamp(datetimefield,'MM-dd-yyyy HH:mm:ss')) >=
date_sub(from_unixtime(unix_timestamp()),1);
We need to calculate the TIME DIFF along with Milliseconds in HIVE Using HUE.
Please find the below screen shot.
select
((unix_timestamp('2017-12-26 14:35:19.609')
- unix_timestamp('2017-12-26 14:35:18.779'))*1000) as timediff
Output :
timediff
1000
In the above case we are getting only Seconds but we are not able to get the Milliseconds Precision.
Could you please provide the solution to achieve this issue using HIVE.(Without Using UDF's in HIVE).
Referring from this answer,for milliseconds, you shouldn't use the unix_timestamp functions because these consider date as seconds since epoch.
How do I get millisecond precision in hive?
So, you could CAST it to TIMESTAMP and then DOUBLE to get the desired result.
SELECT ROUND((CAST(CAST('2017-12-26 14:35:19.609' AS TIMESTAMP) AS DOUBLE)
- CAST(CAST('2017-12-26 14:35:18.779' AS TIMESTAMP) AS DOUBLE)) * 1000)
as timediff
timediff
---------
830