Query by UNIX TIMESTAMP not working in SQLite - sql

I have this query:
select * from applications
where created_at between FROM_UNIXTIME(1270080000) and FROM_UNIXTIME(1554076800)
Which works and returns results as expected. But fails when running tests using sqlite.
SQLSTATE[HY000]: General error: 1 no such function: FROM_UNIXTIME
Is there a different query I can use that will also pass tests?

try like below
select * from applications where created_at between
DATETIME(ROUND(1270080000 / 1000), 'unixepoch') and DATETIME(ROUND(1554076800 / 1000), 'unixepoch')
documentation

Related

I'm trying to calculate a timelapse in SQL but it gives me the result in 'microseconds'

I'm working in a project and in order to know how much time happened between two dates i'm using the following code:
select (DATE_COMPLETED - DATE_STARTED) as avg_days
The problem is that when I run the code it shows the new column but the format answer is for example:
{"months": 0, "days": 0, "microseconds": 2803077000000}
As you may infer I want that information in days, not in 'microseconds'
By the way, the format of the Date data I'm using is '2021-02-24T02:33:00.000+0000'.
Thanks!
If you get microseconds and you want days, use arithmetic:
select (DATE_COMPLETED - DATE_STARTED) / (24 * 60 * 60 * 1000000) as avg_days

Hive how to get current timestamp in format 2020-04-17T19:17:56.017719Z format

I am using Hive and have an important task at hand, which needs the current timestamp in the format 2020-04-17T19:17:56.017719Z.
Any help around exact solution for this in Hive, would be highly appreciated.
Thanks
with time as (
select reflect('java.util.Date','getTime') as millis
)
select concat( from_unixtime(floor(millis / 1000 ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast((millis % 1000)as int),'Z')
from time
Result:
2020-04-26T11:12:35.590Z
Or one more method
select concat(from_unixtime(unix_timestamp(split(current_timestamp,'\\.')[0]),"yyyy-MM-dd'T'HH:mm:ss"),'.',split(current_timestamp,'\\.')[1],'Z')
Result:
2020-04-26T11:28:13.433Z
One more method using regexp_replace:
select regexp_replace(current_timestamp,'(.*) (.*)','$1T$2Z')
Result:
2020-04-26T11:50:51.804Z
If you need to get microseconds in Hive, the dirty trick is
with time as (
select reflect('java.util.Date','getTime') as millis, reflect('java.lang.System','nanoTime') as nano
)
select concat( from_unixtime(floor(millis / 1000 ),"yyyy-MM-dd'T'HH:mm:ss"), '.',cast(millis%1000 as int),cast(nano%1000 as int),'Z')
from time
Result:
2020-04-26T21:53:31.261356Z
But this is not real microsecond-precision.

Does Derby support INTERVAL for date

I am doing performance testing on Apache Derby (10.14.2). I am using TPCH benchmarking for the same. I have completed the dbgen part of the TPCH and populated the database. There are 22 queries in the TPCH benchmarking queries. I am not able to convert the 1st query to suit the syntax of the Apache Derby. In the make file, I gave the DB as DB2. Since there is no Apache Derby option present there.
Query is as follows:
select
l_returnflag,
l_linestatus,
sum(l_quantity) as sum_qty,
sum(l_extendedprice) as sum_base_price,
sum(l_extendedprice * (1 - l_discount)) as sum_disc_price,
sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) as sum_charge,
avg(l_quantity) as avg_qty,
avg(l_extendedprice) as avg_price,
avg(l_discount) as avg_disc,
count(*) as count_order
from
lineitem
where
l_shipdate <= '1998-12-01' - interval ':1' day (3)
group by
l_returnflag,
l_linestatus
order by
l_returnflag,
l_linestatus;
Error from the ij tool:
ERROR 42X01: Syntax error: Encountered "\':1\'" at line 15, column 47.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
Is there a way to generate the queries for Apache Derby in TPCH. Or tool which can covert the queries to Apache Derby.
Thanks in advance.
You can try the TIMESTAMPADD() function:
WHERE l_shipdate <= CAST({fn TIMESTAMPADD(SQL_TSI_DAY, -1, CAST('1998-12-01 00:00:00' AS TIMESTAMP))} AS DATE)

ERROR: function date_trunc(timestamp without time zone) does not exist

I have this problem. I have an sql query am trying to make to my postgres db. These queries work fine in oracle but am in the process of converting it to a postgres query but it complains. This is the query:
select to_char(calldate,'Day') as Day, date_trunc(calldate) as transdate,
Onnet' as destination,ceil(sum(callduration::integer/60) )as total_minutes,round(sum(alltaxcost::integer) ,2)as revenue
from cdr_data
where callclass ='008' and callsubclass='001'
and callduration::integer >0
and regexp_like(identifiant,'^73')
and bundleunits = 'Money'
and inserviceresultindicator in (0,5)
and regexp_like(regexp_replace(callednumber,'^256','') ,'^73')
group by to_char(calldate,'Day') ,trunc(calldate),'Onnet' order by 2
And the error am getting is this:
Err] ERROR: function date_trunc(timestamp without time zone) does not exist
LINE 4: select to_char(calldate,'Day') as Day, date_trunc(calldate)...
What am I doing wrong, or what is the solution to this error?
Try:
... date_trunc('day',calldate) ...
For PostgreSQL date_trunc() function you must always specify precision as the first argument.
Details here.

H2 database. How to convert date to seconds in sql?

Is there analog og the MySQL's time_to_sec() ?
I heed to perform query like the following on H2 database:
select * from order
join timmingSettings on order.timmingSettings = timmingSettings.id
where (order.time-timmingSettings.timeout) < current_timestamp
No, but it seems quite easy to add function to h2 if needed.
To convert a timestamp to seconds since epoch, compile and add a Java class to h2's classpath containing:
public class TimeFunc
{
public static long getSeconds(java.sql.Timestamp ts)
{
return ts.getTime() / 1000;
}
}
The function in the Java code can then be linked in h2 using CREATE ALIAS:
CREATE ALIAS TIME_SECS FOR "TimeFunc.getSeconds";
SELECT TIME_SECS(CURRENT_TIMESTAMP);
Produces:
TIME_SECS(CURRENT_TIMESTAMP())
1255862217
(1 row, 0 ms)
In lieu of adding a function to H2, you can cast the date to a timsestamp and then use formatdatetime per http://www.h2database.com/html/functions.html. Alternatively, cast to string and use parsedatetime. Examples of both follow:
-- to convert using parsedatetime, done_on stores
select parsedatetime(done_on, 'ssss', 'en', 'Europe/Dublin');
-- for this example, assume done_on stores a timestamp
-- to convert using formatdatetime
select formatdatetime(done_on, 'ssss', 'en', 'Europe/Dublin');
Valid timezones can be found in your /usr/share/zoneinfo directory and language codes are per the list at http://en.wikipedia.org/wiki/ISO_639-1 -- the ISO 639 part 1 standard.
I think this is the most simple code.
select DATEDIFF('second',timestamp '1970-01-01 00:00:00' , CURRENT_TIMESTAMP())
SELECT EXTRACT (EPOCH FROM <value>)
So
select * from order
join timmingSettings on order.timmingSettings = timmingSettings.id
where (extract(epoch from order.time)-extract(epoch from timmingSettings.timeout)) < extract(epoch from current_timestamp)