How to compute datediff by hour in impala - impala

How to to compute datediff by hour in impala? For example like below
Select datediff(hour,'2017-06-21T02:29:54.244720804Z', '2017-06-21T02:30:10.574379557Z');
Default impala 'datediff' function only returns diff by day..
Update:
Solution I figured
select cast(abs((unix_timestamp('2017-08-01 01:00:00', 'yyyy-MM-dd HH:mm:ss') - unix_timestamp('2017-08-01 00:00:00', 'yyyy-MM-dd HH:mm:ss'))/3600) as int);

You can apply this workaround,
select datediff(time_a, time_b)*24 + hour(time_a) - hour(time_b);

Related

PostgreSQL BigInt to DateTime and Add hours to get local time

I am trying to convert the bigint value to date in PostgreSQL
I am using the below code
SELECT TO_CHAR(TO_TIMESTAMP(1564983632051/ 1000),
'YYYY-MM-DD HH24:MI:SS')
then its returning 2019-08-05 07:40:32, which is correct.
However i want to add few hours to it to get local time. Tried with the following query but its throwing an error :
SELECT TO_CHAR(TO_CHAR(TO_TIMESTAMP(1564983632051/ 1000),
'YYYY-MM-DD HH24:MI:SS') + INTERVAL '4 hour')
I do not want to use a separate query, if that's the case i can use
select (to_timestamp('2019-08-05 07:40:32', 'YYYY-MM-DD HH24:MI:SS.US') + interval '4 hour')::timestamp;
this will return the desired output.
I need both conversion and hours addition in a single query.
You should add to TIMESTAMP portion, not to portion casted to CHAR :
SELECT TO_CHAR(
TO_TIMESTAMP(1564983632051/ 1000)+ INTERVAL '4 hour',
'YYYY-MM-DD HH24:MI:SS'
)
The problem is here:
select pg_typeof(TO_CHAR(TO_TIMESTAMP(1564983632051/ 1000), 'YYYY-MM-DD HH24:MI:SS'));
pg_typeof
-----------
text
Adding an interval to a text value is not going to work.
So something like:
select TO_CHAR(TO_TIMESTAMP(1564983632051/1000) + interval '4 hour', 'YYYY-MM-DD HH24:MI:SS') ;
to_char
---------------------
2019-08-05 02:40:32
It is best to stay in a type for operations until the very end. Then apply formatting.

How to convert Unix timestamp( 1541107867006) to required date time format in SAP HANA Scripted Calculation View

After a good research and failing to get the required solution, I am posting the question here.
Problem: Unable to convert timestamp '1541107867006' to required date time format in SAP Scripted Calculation View.
Tried the below code and SAP functions but they work for other formats and not this specifically.
SELECT TO_TIMESTAMP (ADD_SECONDS( '1970-01-01 00:00:00', '1541107867006')) TIMESTAMP,
TO_DATE (ADD_SECONDS( '1970-01-01 00:00:00', '1541107867006')) DATE,
TO_TIME (ADD_SECONDS( '1970-01-01 00:00:00', '1541107867006')) TIME
from DUMMY
Can you please help with this? Really appreciate your response!
Thanks
Supriya
This worked for me.
TO_CHAR(ADD_SECONDS(TO_TIMESTAMP('1970-01-01 00:00:00'), cast('1541107867006' as bigint)/1000),'mm/dd/yyyy')
The constant '1541107867006' is not in seconds, but in milliseconds.
Try
SELECT ADD_SECONDS(TO_TIMESTAMP('1970-01-01 00:00:00'),
cast('1541107867006' as bigint)/1000) as "Timestamp"
from DUMMY;
or, because some casts are implicit:
SELECT ADD_SECONDS('1970-01-01 00:00:00',
cast('1541107867006' as bigint)/1000) as "Timestamp"
from DUMMY;
It's easier when the (numeric) Unix timestamp is given as a BIGINT right away:
SELECT ADD_SECONDS('1970-01-01 00:00:00', 1541107867006 / 1000) as "TimeStamp"
from DUMMY;

Oracle Convert Char to Date

I have the following date in oracle table:
'2017-08-01 00:00:00,000000000'
I want to convert this to date which I am using the following but I don't know how to deal with zeroes?!
.
.
.
T.EXECUTION_LOCAL_DATE_TIME
between to_date('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss')
and to_date('2017-08-10 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss');
Could anyone help me with this?
Oracle dates do not support milliseconds. Assuming your EXECUTION_LOCAL_DATE_TIME column is a date, then the following comparison is the best you can do:
T.EXECUTION_LOCAL_DATE_TIME
BETWEEN TO_DATE('2017-08-01 00:00:00', 'yyyy-mm-dd hh:mi:ss') AND
TO_DATE('2017-08-10 00:00:00', 'yyyy-mm-dd hh:mi:ss');
If you wanted to convert a text string with milliseconds to a timestamp, you could use something like this:
TO_TIMESTAMP ('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss,ff')
Generally speaking this values in not a date but a timestamp so I would use following conversion:
select to_timestamp('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh24:mi:ss,ff6')
from dual;
Technically you can have non zeros after comma, so If you want to get correct but not truncated value use timestamp date type.

to_char, to_date in Microsoft SQL Server

I'm trying to convert the following command:
to_char(to_date('01/01/1970 00:00:00', 'MM/DD/YYYY HH24:MI:SS')+((1442998800)/( 60 * 60 * 24 )+(180/1440)),'DD/MM/YYYY HH24:MI:SS')
But with no success, any help is greatly appreciated
Many thanks
Try this:
DECLARE #Input int = 1442998800
SELECT dateadd(second, #Input, '1970-01-01 03:00:00')
see fiddle here.
Explenation: it's clear that the input is the number of seconds since a specific date - turns out that date is January first, 1970, at 3 am.
This works for me:
SELECT FORMAT(DATEADD(SECOND,1442998800,'1970-01-01'),'dd/MM/yyyy hh:mm:ss')
In words' it adds 1442998800 seconds to 1970-01-01 and formats it the way you decsribed. Give it a try ;-)

How to trunc a date to seconds in Oracle

This page mentions how to trunc a timestamp to minutes/hours/etc. in Oracle.
How would you trunc a timestamp to seconds in the same manner?
Since the precision of DATE is to the second (and no fractions of seconds), there is no need to TRUNC at all.
The data type TIMESTAMP allows for fractions of seconds. If you convert it to a DATE the fractional seconds will be removed - e.g.
select cast(systimestamp as date)
from dual;
I am sorry, but all my predecessors seem to be wrong.
select cast(systimestamp as date) from dual
..does not truncate, but rounds to the next second instead.
I use a function:
CREATE OR REPLACE FUNCTION TRUNC_TS(TS IN TIMESTAMP) RETURN DATE AS
BEGIN
RETURN TS;
END;
For example:
SELECT systimestamp
,trunc_ts(systimestamp) date_trunc
,CAST(systimestamp AS DATE) date_cast
FROM dual;
Returns:
SYSTIMESTAMP DATE_TRUNC DATE_CAST
21.01.10 15:03:34,567350 +01:00 21.01.2010 15:03:34 21.01.2010 15:03:35
On the general topic of truncating Oracle dates, here's the documentation link for the format models that can be used in date trunc() AND round() functions
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions242.htm#sthref2718
"Seconds" is not listed because the granularity of the DATE datatype is seconds.
I used function like this:
FUNCTION trunc_sec(p_ts IN timestamp)
IS
p_res timestamp;
BEGIN
RETURN TO_TIMESTAMP(TO_CHAR(p_ts, 'YYYYMMDDHH24MI'), 'YYYYMMDDHH24MI');
END trunc_sec;
trunc work to min only, cast to date to_char(START_TIME,'YYYYMMDDHH24MISS')
or simply select to_char(current_timestamp, 'YYYYMMDDHH24MISS') from dual;
https://www.techonthenet.com/oracle/functions/trunc_date.php
To truncate a timestamp to seconds you can cast it to a date:
CAST(timestamp AS DATE)
To then perform the TRUNC's in the article:
TRUNC(CAST(timestamp AS DATE), 'YEAR')
Something on the order of:
select to_char(current_timestamp, 'SS') from dual;