Cx_oracle: To_date with variables in python - oracle

I am trying to do this
startdate = "20160123"
enddate = "20160204"
cmd = "select identification_number from bug where submitted_date >= TO_DATE(:1,'dd-MON-yy') and submitted_date <= TO_DATE(:2,'dd-MON-yy')"
cursor.execute(cmd,(startdate,enddate))
I get an error
cursor.execute(cmd,(sdate,edate))
cx_Oracle.DatabaseError: ORA-01861: literal does not match format string
I saw previous threads regarding this error but nothing solved my problem

I am not sure how startdate and enddate is translated to :1 and :2 but if it does, then the issue with date format.
You are passing YYYYMMDD and you casting it as DD-MON-YYYY . Try to change it.
Also you are missing from clause.
And I'd used between clause instead.
select identification_number
from <your_table>
where
submitted_date between
TO_DATE(:1,'YYYYMMDD') and TO_DATE(:2,'YYYYMMDD')
If this works, then use same date format in your code
startdate = "20160123"
enddate = "20160204"
cmd = "select identification_number from <your_table> where submitted_date between TO_DATE(:1,'YYYYMMDD') and TO_DATE(:2,'YYYYMMDD')"

Related

Proc SQL DATE() in where statement

I'm trying to edit this code to be dynamic as I'm going to schedule it to run.
Normally I would input the date in the where statement as 'YYYY-MM-DD' and so to make it dynamic I changed it to DATE(). I'm not erroring out, but I'm also not pulling data. I just need help with format and my google searching isn't helping.
PROC SQL;
CONNECT TO Hadoop (server=disregard this top part);
CREATE TABLE raw_daily_fcast AS SELECT * FROM connection to Hadoop(
SELECT DISTINCT
a.RUN_DATE,
a.SCHEDSHIPDATE,
a.SOURCE,
a.DEST ,
a.ITEM,
b.U_OPSTUDY,
a.QTY,
c.case_pack_qty
FROM CSO.RECSHIP a
LEFT JOIN CSO.UDT_ITEMPARAM b
ON a.ITEM = b.ITEM
LEFT JOIN SCM.DIM_PROD_PLN c
ON a.ITEM = c.PLN_NBR
WHERE a.RUN_DATE = DATE()
AND a.SOURCE IN ('88001', '88003', '88004', '88006', '88008', '88010', '88011', '88012',
'88017', '88018', '88024', '88035', '88040', '88041', '88042', '88047')
);
DISCONNECT FROM Hadoop;
QUIT;
When RUN_DATE is a string you can generate the current date string in-line on the SAS side
WHERE a.RUN_DATE = %str(%')%sysfunc(date(),yymmdd10.)%str(%')
AND ...
or
WHERE a.RUN_DATE = %sysfunc(quote(%sysfunc(date(),yymmdd10.),%str(%')))
AND ...
For the case of RUN_DATE being a string containing DATE9 formatted values, change the yymmdd10. to date9.
change:
WHERE a.RUN_DATE = DATE()
to:
WHERE a.RUN_DATE = PUT(date(), YYMMDD10.) AS date

Spring Boot data-jpa and nativeQuery Postgres cast

I have a custom #Query in a repository that looks like this:
SELECT * FROM topicaudit_c14001
WHERE auditdate >= NOW()
AND auditdate <= NOW() + '1 hour'::INTERVAL
AND accepted_status = 'ACCEPTED'
AND reminder_sent = FALSE
When I run this, I get the exception:
org.hibernate.QueryException:
Not all named parameters have been set: [:INTERVAL]
Obviously it is interpreting the ::INTERVAL cast (Postgresql) as a named parameter and cannot fire the query since I don't provide a parameter.
How can I write this query so that it works with JPA?
I found it out shortly after posting. Escaping the :: helps.
SELECT * FROM topicaudit_c14001
WHERE auditdate >= NOW()
AND auditdate <= NOW() + '1 hour'\\:\\:INTERVAL
AND accepted_status = 'ACCEPTED'
AND reminder_sent = FALSE

Passing Date to NamedParameterJdbcTemplate in Select query to oracle

I have a query as below which is returning expected records when run from the SQL Developer
SELECT *
FROM MY_TABLE WHERE ( CRT_TS > TO_DATE('25-Aug-2016 15:08:18', 'DD-MON-YYYY HH24:MI:SS')
or UPD_TS > TO_DATE('25-Aug-2016 15:08:18', 'DD-MON-YYYY HH24:MI:SS'));
I think that we will not need to apply TO_DATE when we are passing java.util.Date object as date parameters but the below code snippet is silently returning me 0 records.
My SQL query in Java class is as below:
SELECT *
FROM MY_TABLE WHERE ( CRT_TS > :lastSuccessfulReplicationTimeStamp1
or UPD_TS > :lastSuccessfulReplicationTimeStamp2);
The code which executes the above query is as below but the below code snippet is silently returning me 0 records:
parameters.put("lastSuccessfulReplicationTimeStamp1", new java.sql.Date(outputFileMetaData.getLastSuccessfulReplicationTimeStamp().getTime()));
parameters.put("lastSuccessfulReplicationTimeStamp2", new java.sql.Date(outputFileMetaData.getLastSuccessfulReplicationTimeStamp().getTime()));
list = namedParameterJdbcTemplateOracle.query(sql, parameters, myTabRowMapper);
Please advise.
I guess you already found the answer but if anybody else needs it, here's what I've found:
java.sql.Date doesn't have time, just the date fields. Either use java.sql.Timestamp or java.util.Date. Both seems to be working for me with NamedParameterJdbcTemplate.
A little variation to above solution can be when your input(lastSuccessfulReplicationTimeStamp1/lastSuccessfulReplicationTimeStamp2) is a String instead of Date/TimeStamp (which is what i was looking for and found at this link -> may be it can help someone):
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("lastSuccessfulReplicationTimeStamp1", lastSuccessfulReplicationTimeStamp1, Types.TIMESTAMP);
parameters.addValue("lastSuccessfulReplicationTimeStamp2", lastSuccessfulReplicationTimeStamp2, Types.TIMESTAMP);
list = namedParameterJdbcTemplateOracle.query(sql, parameters, myTabRowMapper);

Issue in date conversion in ruby

In my #query I am putting the date in MM-DD-YYYY format. e.g., 01-22-2016. Now I want to search the dates from messages table with that date how can I do. I had added the query:
Message.includes(:user)
.select('messages.*, users.name')
.where(
"users.name ilike ? OR messages.to ilike ? OR messages.created_at.srtftime(\"%Y-%m-%d\") = ?",
"%#{#query}%",
"%#{#query}%",
Date.strptime(#query, "%m-%d-%Y")
)
But I am getting the issue for that:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "%Y-%m-%d" does not exist
I had also added the query as:
Message.includes(:user)
.select('messages.*, users.name')
.where(
"users.name ilike ? OR messages.to ilike ? OR messages.created_at.srtftime("%Y-%m-%d") = ?",
"%#{#query}%",
"%#{#query}%",
Date.strptime(#query, "%m-%d-%Y")
)
But in that the error is coming as: unknown type of %string
From your comment on #Amadan's answer:
If I don't convert my message.created_at then I am getting blank result.
Message.includes(:user)
.select('messages.*, users.name')
.where(
"users.name ilike ? OR messages.to ilike ? OR messages.created_at = ?",
"%#{#query}%",
"%#{#query}%",
Date.strptime(#query, "%m-%d-%Y")
)
This will produce a query with (among others) a condition like this:
messages.created_at = '2016-01-29 00:00:00'
It should be apparent why this won't work.
messages.created_at is a timestamp, and unless the record just happens to have been created at midnight, that equality comparison is going to fail.
What you need to do is compare the date part of messages.created_at to the date from the query. To get the date part of a timestamp in PostgreSQL, you can use either the date() function or the ::date suffix:
Message.includes(:user)
.select('messages.*, users.name')
.where(
"messages.created_at::date = ?",
Date.strptime(#query, "%m-%d-%Y")
)
Hopefully, you don't have dates stored in the database in VARCHAR columns, but DATETIME or equivalent. Don't convert to String, let ActiveRecord handle dates for you: messages.created_at = ?.

Error in creating query in openbravo through hql

This question might have answer ... But not for openbravo with postgresql database.
I have openbravo 3.0 framework. In my window i have two date fields namely fromdate and todate. The requirement is i have to write a hql where clause to filter the records on the basis of current date.The date field is of timestamp without timezone.
Means fromdate < currentdate
and todate > currentdate .
I went through this link and wrote the hql where clause as
e.id in(select s.id from Tablename as s where s.fromdate < current_Date and s.todate>current_date)
when i open this window i get this error as
Exception when creating query select e from Tablename as e
where ( e.id in(select s.Tablename_ID from Tablename as s where s.fromdate < (current_date) and s.todate < (current_date)
however if i remove the current date conditions as
e.id in(select s.id from Tablename as s).. It is working fine.
Is it because of current_Date function ? .I tried even with now function .. but i get the same error.
!!! Got The Error.
There is a problem in the query i wrote , in the where clause i am selecting the id's which is not correct,hence when i gave the below query it was running correctly.
(Tablename.fromdate < currrent_date and TableName.todate>current_date) There was no problem with the current_date function.
I thought may be it would help some one!!!
Tip: If you want to write hql query correctly in openbravo , pls install the Hql query tool module that is available freely for community edition of openbravo.
Happy Coding

Resources