Does Derby support INTERVAL for date - sql

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)

Related

Query by UNIX TIMESTAMP not working in SQLite

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

DATEDIFF in DB2 query

I have a query from mysql that has been running on a table I recently migrated to DB2.
The query now fails on DB2 due to the line below, saying that DATEDIFF cannot be found. I'm assuming only because this isn't a valid function on db2.
Is there an equivalent to this on DB2 that will maintain performance as well as function?
SELECT DISTINCT
LEAST(180, DATEDIFF(curdate(), start_date)) as days
FROM table2
where expire_date > curdate()
I use the DAYS() function to convert the dates to numeric sequential numbers and then just subtract them, as in:
SELECT DISTINCT
LEAST(180, DAYS(curdate()) - DAYS(start_date)) as days
FROM table2
where expire_date > curdate()
According to DB2's manual, DAYS() returns: "The result is 1 more than the number of days from January 1, 0001".
On Db2 11.1 (for Linux, Unix and Windows) and above, this will work
SELECT DISTINCT
LEAST(180, DAYS_BETWEEN(current_date, start_date)) as days
FROM table2
where expire_date > current_date

SQL Server gives syntax error

I have following SQL for Microsoft SQL Server
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN DATE'2015-09-12' AND DATE'2015-09-15'
But this throws a syntax error:
Error: Incorrect syntax near '2015-09-12'. SQLState: S0001 ErrorCode:
102
What is wrong? I want to use ANSI literal code.
This is not how you cast in MS SQL Server. Instead, you should try the cast syntax:
SELECT *
FROM tblA
WHERE creation_date BETWEEN CAST('2015-09-12' AS DATE) AND
CAST('2015-09-15' AS DATE)
You can simply use this query, without adding date:
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN '2015-09-12' AND '2015-09-15'
Date literals are tricky... besides the problem with conversion (often depending on your system's culture) you must think of the day's end if you use between!
Here you find a system independent way to literally write dates: https://msdn.microsoft.com/en-us/library/ms187819.aspx?f=255&MSPPError=-2147217396 (look at "ODBC)
So you could write
BETWEEN {d'2015-09-12'} AND {ts'2015-09-12 23:59:59'}
SELECT *
FROM tblA
WHERE CREATION_DATE BETWEEN CAST('2015-09-12' AS DATE)
AND CAST('2015-09-15' AS DATE)

SQL Query Current Date Minus 180 days?

Query
I'm getting a syntax error:
SELECT "LastFilterDate"
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" < DATE_SUB(CURDATE(),INTERVAL 180 DAY)
I want to select the LastFilterDateS that are older than the current date minus 180 days (6 months).
I'm using LibreOffice base.
error in question:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or
SQL_TOKEN_LIKE
Sandesh gave a fantastic link on the subject, but I'm still getting a syntax error.
variations I've tried:
SELECT *
FROM Filter Cartridge Updates
WHERE LastFilterDate BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
SELECT *
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
The problem is that you're using MYSQL syntax for an SQL that is not MYSQL.
You can find some help on the LibreOffice website. I found this: =DATEDIF("1974-04-17";"2012-06-13";"d") which yields the number of days between 2 dates.
So you could change your where clause to be (if this allows substitution - you'll have to test that), to:
Where =DATEDIF(last_filter_date,current_date;"d") > 180
Note: I only put stubs in for the dates, you'll have to format those correctly.

sql query to get last 6 months of data

I am trying to get the data for the last 6 months.
This is what I have used:
WHERE d_date > DATEADD(m, -6, current_timestamp)
and I am getting this error.
ERROR: CLI prepare error: SQL0206N "M" is not valid in the context where it is used
also tried
WHERE d_date > current date -180
and got this error:
ERROR: CLI prepare error: SQL0171N The data type, length or value of the argument for the parameter in
position "2" of routine "-" is incorrect. Parameter name: "". SQLSTATE=42815
Please advice.
Based on Andriy's eagle-eyes, here is (I think) the DB2 syntax:
WHERE d_date > current_date - 6 MONTHS
And here is a link to a pretty good function to mirror DATEADD in DB2.
Also, since you mentioned SAS, here is the SAS syntax to do the same thing:
WHERE d_date > intnx('MONTH', today(), -6, 'SAME');
Although you say you are running this with SAS Enterprise Guide, the syntax you show is not SAS. The error message you are getting suggests you are submitting "pass-thru" code directly to a database.
In DB2, it should be something like
WHERE TIMESTAMPDIFF(64, CAST(CURRENT_TIMESTAMP- d_date AS CHAR(22))) <= 6
Remove the SQL-Server tag, that's for MS SQLServer questions.
You can also try this
SELECT *
From PIS.dbo.PIS_tblEmployeeGenInfo
WHERE dtJoiningDate > DATEADD(m, -6, current_timestamp)
I'm using in code the following:
INSERT INTO Employee(I_EMP, T_CRT, C_SYS, D_HIRED)
VALUES (21,CURRENT TIMESTAMP, 10,CURRENT DATE - 6 MONTH);
This is in DB2, and it is working with no issues.
Hope that will help.