How to add Days to a current Date in h2 database? - db2

Query is executing in DB2 DB but its failing in H2 database:
SELECT P.CC_ID, MIN(P.CC_EXP_DT) CC_EXP_DT, MIN(P.PAYMT_TYP_CD) PAYMT_TYP_CD
FROM Q.PAYMT P
WHERE (P.PAYMT_BILLSTAT_CD = 'PD'
OR (P.PAYMT_DUE_DT <= CURRENT DATE + CAST( 10 AS INT ) DAYS
AND P.PAYMT_BILLSTAT_CD IN ('FB', 'RR', 'RP'))) AND (P.CC_ID IS NOT NULL)
AND (P.INSTR_TYP_CD = 'EP') AND (P.PAYMT_TYP_CD IN ('VI', 'MC'))
GROUP BY P.CC_ID
Error in H2 DB like :
Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Syntax
error in SQL statement "SELECT P.CC_ID, MIN(P.CC_EXP_DT) CC_EXP_DT,
MIN(P.PAYMT_TYP_CD) PAYMT_TYP_CD FROM Q.PAYMT P WHERE
(P.PAYMT_BILLSTAT_CD = 'PD' OR (P.PAYMT_DUE_DT <= CURRENT DATE +
CAST( 10 AS INT ) DAYS[*] AND P.PAYMT_BILLSTAT_CD IN ('FB', 'RR',
'RP'))) AND (P.CC_ID IS NOT NULL) AND (P.INSTR_TYP_CD = 'EP') AND
(P.PAYMT_TYP_CD IN ('VI', 'MC')) GROUP BY P.CC_ID"; expected "[, ::,
*, /, %, +, -, ||, ~, !~, NOT, LIKE, ILIKE, REGEXP, IS, IN, BETWEEN, AND, OR, ,, )"; SQL statement:
here DAYS[*] function in not available in H2 DB, please help me to execute above query

Related

HQL / Postgres : EXTRACT(dow from CURRENT_TIMESTAMP) with a parameter

I am trying to implement following query to retrieve users that need to be alerted weekly and send alerts on Monday.
Following query works :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from CURRENT_TIMESTAMP) = 1))";
But if I want to pass a parameter to EXTRACT(dow from CURRENT_TIMESTAMP) like this :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from : currentTs) = 1))";
var args = new MapSqlParameterSource("currentTs", currentTs);
jdbcOperations.query(sql, args, (ResultSet rs) -> ...);
I am getting following error :
nested exception is org.postgresql.util.PSQLException: ERROR: function pg_catalog.date_part(unknown, unknown) is not unique
Hint: Could not choose a best candidate function. You might need to add explicit type casts.
Not sure how I can pass a parameter to EXTRACT(dow from ...) ?
Any pointers would be appreciated.
Thank you.

HQL / Postgres : ERROR: operator does not exist: timestamp with time zone + integer

I am trying to familiarise myself with Hql, Postgres and JDBC queries. I have a simple query to retrieve records from a table that were modified in last week :
String query = "SELECT t.id FROM table t"
+ " WHERE CURRENT_TIMESTAMP <= t.last_modified_timestamp + 7";
but getting this exception:
nested exception is org.postgresql.util.PSQLException: ERROR: operator
does not exist: timestamp with time zone + integer
Did I miss something? I probably missed the right command to do so.

case when in where clause postresql

select service_code,service_cat_code,mobile_no,upper(applicant_name_eng) as name,to_char(license_date,'dd/mm/yyyy')as license_from,to_char(license_valid_upto,'dd/mm/yyyy')as license_to,Upper(license_no),district_code,taluk_code,CONCAT(address_building,', ', address_cityvillage,', ',address_locality,', ',address_landmark,', ',address_street) as address
from mst_license
WHERE cast(license_valid_upto as date) = case
WHEN license_valid_upto < now()
THEN
case
when license_valid_upto = '2021-06-30'
then 1 else 0
END
ELSE
case when license_valid_upto > now()
then 1 else 0
End
END
and Upper(license_no)='1SP146924BJP'
I want license valid should be either greater than now or if license valid less than now it must be with the date ''30/06/2021' but when i use above query i get error
ERROR: operator does not exist: date = integer
LINE 3: WHERE cast(license_valid_upto as date) = case
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 418
Help me out guys
The main issue you have is that your case statement returns an integer (1 or 0) but you are trying to compare that to a date, which you cannot do as Postgres is a strict data typing. Even if it did work it would always be false (well except for 1969-12-31/1970-01-01). Moreover the case structure is not needed. The best/correct to compare dates is just use date values. Since you did not indicate the data type for column license_valid_upto so based on how it is used I'll assume it is timestamp with timezone as that is what NOW() returns. Your query becomes:
select service_code
, service_cat_code
, mobile_no
, upper(applicant_name_eng) as name
, to_char(license_date,'dd/mm/yyyy')as license_from
, to_char(license_valid_upto,'dd/mm/yyyy')as license_to
, upper(license_no) as license_no
, district_code
, taluk_code
, concat(address_building,', '
,address_cityvillage,', '
,address_locality,', '
,address_landmark,', '
,address_street) as address
from mst_license
where and upper(license_no)='1SP146924BJP'
and ( cast(license_valid_upto as date) > cast( now() as date)
or (cast (icense_valid_upto as date) < cast( now() as date)
and cast (icense_valid_upto as date) = date '2021-06-30'
)
);
Also, learn for format your queries for readability and so you do not need to scroll right. You, and others looking at your queries later will appreciate it later.

Why does the gorm postgresql throws pq: syntax error at or near ")"?

SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime)
var v = request.GetVIds()
if len(v) > 0 {
query = query.Where(` v_id IN (?) `, v)
} //Only this block introduces first ) after end_time
var c = request.GetStatus().String()
if len(c) > 0 {
query = query.Where( " status = ? ", c) // this introduces the other opening brace //after AND
}
Following is the query generated and found in logs
SELECT * FROM events WHERE c_id = 1 AND start_time > '2020-04-16 18:42:00' and
end_time < '2020-04-16 18:45:50' ) AND ( v_id IN (1,2)) AND ( status = 'STATUS_MIDDLE_CLASS' ORDER BY start_time DESC LIMIT 5 OFFSET 1
The other solution in stackoverflow and internet article doesn't help.
PS: Is it because I mix db.Raw( ) and query.Where() ?
Changing ? to $1 doesn't fix the issue.
Basically a few things fixed the issue.
1) Mixing Raw and query.Where was one issue.
After making the Raw query to sr.db.Where
2)
SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
already has select * from. And then using query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime) introduces nested select *.
So, changed SELECT_QUERY as follows
SELECT_QUERY = `events WHERE c_id = ? AND start_time > ? and
end_time < ?`
solved the issue.
I found a workaround to the error which I received when I tried to add a timestamp filed in Go/Gorm solution with PostgreSQL equivalent to default: now() which is default: time.Now().Format(time.RFC3339)
I received the error because I use AutoMigrate() to create the tables in PostgreSQL. The one problem I found is when trying to use the default value of a function instead of a string (which one can use for a fixed timestamp).
So I had to go into DataGrid (since I use JetBrains, but you can use any PostgreSQL admin tool like pgAdmin) and manually add the timestamp field with default of now() or merely update the existing field to add the default of now(). Then the error goes away when doing your next build in Go.

intersystem cache C# query with datetime

When I use cache sql query in C# I'm getting an error:
SQLtext1 = "SELECT top 10 * FROM dbo.DAPPLICATIONSTAT where TIMESTAMP = '2015-02-01 00:00:00'"
I would like to use a where clause with a datetime filter.
I am using InterSystems.Data.CacheClient.dll to execute the query.
Error Messge :
[SQLCODE: <-4>:<A term expected, beginning with one of the following: identifier, constant, aggregate, %ALPHAUP, %EXACT, %MVR, %SQLSTRING, %SQLUPPER, %STRING, %UPPER, $$, :, +, -, (, NOT, EXISTS, or FOR>]
[Cache Error: <<SYNTAX>errdone+2^%qaqqt>] [Details: <Prepare>]
[%msg: < SQL ERROR #4: A term expected, beginning with either of: (, NOT, EXISTS, or FOR^SELECT top :%qpar(1) * FROM dbo . DAPPLICATIONSTAT where TIMESTAMP>
I think that you have reserved word TIMESTAMP and so, you have that error
Try this SQL query, where filedname TIMESTAMP in dobled quotas
SELECT top 10 * FROM dbo.DAPPLICATIONSTAT where "TIMESTAMP" = '2015-02-01 00:00:00'