Get delta of current date and column's timestamp - does not exist: double precision - timestamp without time zone - postgresql-9.5

columns created_at, modified_at type timestamp without timezone
Get delta in in this columns in milliseconds:
select extract(milliseconds from modified_at - created_at) as delta_millsfrom shop_order
Nice. Now I want to get delta current date and column created_at
I try this:
select id, extract(milliseconds from (EXTRACT(EPOCH FROM current_timestamp) * 1000) - created_at) as detla_mills from shop_order
But get error
:
ERROR: operator does not exist: double precision - timestamp without time zone
LINE 1: ...om (EXTRACT(EPOCH FROM current_timestamp) * 1000) - created_...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
SQL state: 42883
Character: 85

From the text of the error and examining the syntax of the failing expression, the problem is that EXTRACT(EPOCH FROM current_timestamp) is probably not a timestamp type, and definitely multiplying it by * 1000 can't sensibly result in a timestamp type (what date would be (Sep 16, 2020) times 1000, eh?). According to the error, it's a double precision, from which you can't then subtract a timestamp.
You need to subtract some base timestamp value from current_timestamp and then convert it to another type or part that you want, or else convert the base and current_timestamp in the same way to another type that can be directly subtracted for the delta (such as converting the extracted EPOCH back to a base timestamp at the start of that EPOCH.

Related

How to substract 2 varchar dates in oracle?

I have these varchar : 20211026231735.
So I would like a query to substract actual sysdate to that date and convert the substraction to DAY HOURS AND SECONDS.
select TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS') - start_time from TABLEA where job_name='jOB_AA_BB';
I get 4220.
Any help please? Thanks
When you do datetime arithmetic with the DATE datatype, you get back a NUMBER of days. To get an INTERVAL you can subtract two TIMESTAMPs. You don't say what the data type is for start_time, but you might get away with this:
select localtimestamp - start_time
from tablea where job_name='jOB_AA_BB';
LOCALTIMESTAMP gives you a TIMESTAMP value in the current session time zone. There's also CURRENT_TIMESTAMP, which give you the same thing in a TIMESTAMP WITH TIME ZONE and SYSTIMESTAMP that gives you the database time in TIMESTAMP WITH TIME ZONE. You may need to convert your start_time to avoid time zone differences, if any.
You can us the function numtodsinterval to convert the results of date arithmetic to an interval. If necessary then use extract to pull out the needed components.
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select numtodsinterval((SYSDATE - to_date( start_time,'yyyymmddhh24miss')),'hour') date_diff
from tablea where job_name='jOB_AA_BB' ;
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select extract (hour from date_diff) || ':' || extract (minute from date_diff)
from (
select numtodsinterval((sysdate - to_date( start_time,'yyyymmddhh24miss')),'day') date_diff
from tablea where job_name='jOB_AA_BB'
);
NOTE: I am not sure how you got any result, other than an error, as your query winds up as a string - a string. You should not convert sysdate to a string but your string to a date (better yet store it as the proper data type - date).
You can convert the value to a date (rather than converting SYSDATE to a string) and then subtract and explicitly return the value as an INTERVAL DAY TO SECOND type:
SELECT (SYSDATE - TO_DATE('20211026231735', 'YYYYMMDDHH24MISS')) DAY TO SECOND
FROM DUAL;
Or, for your table:
SELECT (SYSDATE - TO_DATE(start_time,'YYYYMMDDHH24MISS')) DAY(5) TO SECOND
FROM TABLEA
WHERE job_name='jOB_AA_BB';
db<>fiddle here

Subtracting Day from Postgresql EPOCH Column

I have the following code where I am trying to simply subtract 5 days from the date. The date is stored as in EPOCH time (miliseconds, 13 numbers)in the t.Date_created field. but for some reason the code does not work with the following error. Any advice would be helpful!!!
[42883] ERROR: operator does not exist: timestamp with time zone -
integer Hint: No operator matches the given name and argument types.
You might need to add explicit type casts.
Code below:
SELECT to_timestamp(t.date_Created / 1000) - 5 FROM task_mgmt.teams t LIMIT 5;
You need to subtract an interval
to_timestamp(t.date_Created / 1000) - interval '5 days'
Integers can only be subtracted directly from a date value, not from a timestamp value.
I would suggest arithmetic:
t.date_created - 5 * 24 * 60 * 60 * 1000

HSQLDB (HyperSQL) - how to get UNIX timestamp as a number with ms precision

I'm trying to get a Java-style long timestamp, that is, UNIX timestamp with millisecond precision * 1000, which fits into a non-floating-point type (BIGINT).
I didn't find a way to get it straight from some function, like CURRENT_TIMESTAMP, unless I was okay with formatting like 20181010123059123.
So I found that this would give me something that looks like a number:
(CURRENT_TIMESTAMP - TIMESTAMP '2018-01-01 00:00:00') SECOND TO SECOND
-- Gives: 23649115.452000
Note that I am substracting 2018-... since I only care about the delta, not the absolute date.
I am not sure if this is the simples way.
Turns out the type of this is INTERVAL, so I need to convert:
CAST(
(CURRENT_TIMESTAMP - TIMESTAMP '2018-01-01 00:00:00') SECOND TO SECOND
AS DECIMAL(15,3)
)
-- Gives 23649115.000
Now the issue is, the precision is lost.
So I wonder: Where is the .452 lost and how can I keep it? This is what the manual says:
An interval value can be cast to a numeric type. In this case the interval value is first converted to a single-field INTERVAL type with the same field as the least significant filed of the interval value. The value is then converted to the target type. For example CAST (INTERVAL '1-11' YEAR TO MONTH AS INT) evaluates to INTERVAL '23' MONTH, and then 23.
And the ultimate question is:
How can I get UNIX timestamp-like number of milliseconds since some moment, e.g. UNIX epoch start?
My current whole SQL:
SELECT
(CURRENT_TIMESTAMP - TIMESTAMP '2018-01-01 00:00:00') SECOND TO SECOND,
FLOOR(
CAST(
(CURRENT_TIMESTAMP - TIMESTAMP '2018-01-01 00:00:00') SECOND TO SECOND
AS DECIMAL(15,3)
) * 1000
)
FROM (VALUES(0));
-- Gives: 23649115.452000 | 23649115000
Turns out there's UNIX_MILLIS which I overlooked (because the broken PDF format manual prevents proper searching).
SELECT UNIX_MILLIS() FROM (VALUES(0))
Which renders my attempts above a nice excercise in intervals.
I still wonder, how should I CAST an interval to retain the milliseconds part.

how to convert a timestamp to int in sql (vertica)

I have a timestamp as 2017-07-19 11:45:01and i want it to convert to int.
Query:
select cast(max(event_timestamp) as INT) from error_messages where error_level='ERROR' and user_name='git'
Error:
SQL Error [2366] [42846]: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
[Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
com.vertica.util.ServerException: [Vertica][VJDBC](2366) ERROR: Cannot cast type timestamptz to int
You have to use TIMESTAMPDIFF() this way:
SELECT TIMESTAMPDIFF(SECOND,'001-01-01 00:00:00', '2015-02-23 03:12:35');
timestampdiff
---------------
63560257955
to get the number of time units you want (SECONDs here above) since the timestamp you want...
If you want to get Unix Timestamp of that date as int than search fort that.
One option would be to calculate the range from your date to '1970-01-01' in seconds as int. This is the Unix Timestamp.
Use JULIAN_DAY function in Vertica to convert the time stamp to a integer value or number.
For more details refer Vertica documentation link: https://my.vertica.com/docs/6.1.x/HTML/index.htm#16070.htm
To extract number from date time with 1 second interval.
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');

Hive from_unixtime for milliseconds

We have a timestamp epoch column (BIGINT) stored in Hive.
We want to get Date 'yyyy-MM-dd' for this epoch.
Problem is my epoch is in milliseconds e.g. 1409535303522.
So select timestamp, from_unixtime(timestamp,'yyyy-MM-dd') gives wrong results for date as it expects epoch in seconds.
So i tried dividing it by 1000. But then it gets converted to Double and we can not apply function to it. Even CAST is not working when I try to Convert this double to Bigint.
Solved it by following query:
select timestamp, from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd') from Hadoop_V1_Main_text_archieved limit 10;
The type should be double to ensure precision is not lost:
select from_unixtime(cast(1601256179170 as double)/1000.0, "yyyy-MM-dd hh:mm:ss.SSS") as event_timestamp
timestamp_ms is unixtime in milliseconds
SELECT from_unixtime(floor(CAST(timestamp_ms AS BIGINT)/1000), 'yyyy-MM-dd HH:mm:ss.SSS') as created_timestamp FROM table_name;
In the original answer you'll get string, but if you'd like to get date you need to call extra cast with date:
select
timestamp,
cast(from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd') as date) as date_col
from Hadoop_V1_Main_text_archieved
limit 10;
Docs for casting dates and timestamps. For converting string to date:
cast(string as date)
If the string is in the form 'YYYY-MM-DD', then a date value corresponding to that year/month/day is returned. If the string value does not match this formate, then NULL is returned.
Date type is available only from Hive > 0.12.0 as mentioned here:
DATE (Note: Only available starting with Hive 0.12.0)