add dates to a bind variable date field - oracle

We are using oracle 12c forms and reports. In one of the query the users will enter the date through forms and the below mentioned query will fetch the required data.
SELECT COUNT(*)
FROM T_APPLICATION_HDR,T_APPLN_PENSIONER
WHERE APPLN_PK=APEN_APPLN_PK
AND APPLN_DATE <=:APPLN_DATE
AND APPLN_SECN_ID ='PV1'
AND APPLN_STAT='04'
What i require is from the above query the data fetched will be prior to the date entered by the users. I want to add 10 days to the date entered by users and records populated based on that date.
i modified the above query like this
SELECT COUNT(*)
FROM T_APPLICATION_HDR,T_APPLN_PENSIONER
WHERE APPLN_PK=APEN_APPLN_PK
AND APPLN_DATE <=:APPLN_DATE+10
AND APPLN_SECN_ID ='PV1'
AND APPLN_STAT='04'
It gives this error
ORA-00932: inconsistent datatypes: expected DATE got NUMBER.
How do i modify this query
i modified the above query like this
SELECT COUNT(*)
FROM T_APPLICATION_HDR,T_APPLN_PENSIONER
WHERE APPLN_PK=APEN_APPLN_PK
AND APPLN_DATE <=:APPLN_DATE+10
AND APPLN_SECN_ID ='PV1'
AND APPLN_STAT='04'
For example if the appln_date entered is 10-may-2019, then the query should fetch records prior to 20-may-2019.
It gives this error
ORA-00932: inconsistent datatypes: expected DATE got NUMBER.
How do i modify this query

'10-may-2019' is not a date, it's text string. In the first case Oracle is smart enough to convert the text string to a date. In the first case Oracle is able to do the conversion but in the second case it gets confused.
I suggest changing your code to be
SELECT COUNT(*)
FROM T_APPLICATION_HDR,T_APPLN_PENSIONER
WHERE APPLN_PK=APEN_APPLN_PK
AND APPLN_DATE <= TO_DATE(:APPLN_DATE, 'DD-MON-YYYY') + INTERVAL '10' DAY
AND APPLN_SECN_ID ='PV1'
AND APPLN_STAT='04'

Related

Getting Oracle Error While trying to convert varchar2 column and retrieve data using BIP report

We have column (field) attribute14 in the oracle table which is of data type VARCHAR2 and is storing date in format '20-MAY-22' (DD-MON-YY).
We have a requirement to develop a BIP report in oracle fusion and get output based on the parameter date passed & if no parameter is based then all the records.
But as we all know any date parameter in the oracle Data set or BIP report will be in the below format.
Date Parameter: '2021-12-05T19:00:00.000-05:00'
And upon trying the below query when the parameter is passed the following error.
select *
from ab cet
where 1=1
AND TO_DATE(cet. attribute14, DD-MON-YY NLS_date_language=AMERICAN")
IN ( nvl(:void_date.TO_DATE(cet.attribute14, DD-MON-YY.NLS_date_language=AMERICAN")))
The below error comes when the parameter value is passed.
Error: ORA-01847: day of the month must be between 1 and the last day of the month.
You need to use single quotes around the date format. Seems your language is also incorrect. Details can be read in the Oracle documentation. Here is a simple example.
SELECT TO_DATE(
'20-MAY-22',
'DD-MON-YY',
'NLS_DATE_LANGUAGE = American')
FROM DUAL;
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm

ElasticSearch SQL API format dateime

I connect to ElasticSearch via (https://www.elastic.co/guide/en/elasticsearch/reference/master//sql-jdbc.html). so that my application server can generate queries similar to SQL syntax. SQL API syntax is understandable, but I can't figure out how to change the format of the returned date. eg: request:
select "#timestamp" from a_nlb
limit 10
Returns a format
YYYY-MM-DD HH:mm:ss.s
How to bring the output to the format DD-MM-YYYY HH:mm:ss?
trunc and to_date are absent
from the doc
https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html#sql-functions-datetime-datetimeformat
SELECT DATETIME_FORMAT(CAST('2020-04-05' AS DATE), 'dd/MM/YYYY') AS "date";
date
------------------
05/04/2020

ORACLE 11g SQL date query not returning data for today

I have the following SQL which should return all data up to 6.30am tomorrow. It has been working correctly until today (30/6/2016).
SELECT TO_CHAR(TRUK.THEDATE,'DD/MM/YY HH24:MI') DAT
FROM TRUK
WHERE TO_CHAR(TRUK.THEDATE,'DD/MM/YY HH24:MI') <= TO_CHAR(TO_DATE(sysdate + 1) + 6.5/24,'DD/MM/YY HH24:MI')
ORDER BY TRUK.THEDATE
PROBLEM
Today the data returned does not include data for 30th June,which i know exists but only 1st July. When i comment out the where clause, then all data is returned but of course this includes data AFTER 6.30am for the next day which i don't want returned.
I have searched in vain for an answer and would greatly appreciate some assistance with this. How Could the where clause be written differently to prevent this?
My desired result is that all records in the database are returned up to 6.30am the following day.
cheers
You're comparing strings, but you should be comparing dates.
Adjust your where clause to this and it'll work:
WHERE truk.thedate <= trunc(sysdate) + to_dsinterval('1 06:30:00')

timestamp not working in hive

I have a table with one column data type as 'timestamp'. Whenever i try to do some queries on the table, even simple select statement i am getting errors.
example of a row in my column,
'2014-01-01 05:05:20.664592-08'
The statement i am trying,
'select * from mytable limit 10;'
the error i am getting is
'Failed with exception java.io.IOException:java.lang.NumberFormatException: For input string: "051-08000"'
Date functions in hive like TO_DATE are also not working.If i change the data type to string, i am able to extract the date part using substring. But i need to work with timestamp.
Has anyone faced this error before? Please let me know.
Hadoop is having trouble understanding '2014-01-01 05:05:20.664592-08' as a timestamp because of the "592-08" at the end. You should change the datatype to string, cut off the offending portion with a string function, then cast back to timestamp:
select cast(substring(time_stamp_field,1,23) as timestamp) from mytable

Comparing Date column to sysdate yields: a non-numeric character was found where a numeric was expected

I've been having a strange issue where the comparison of a date column to SYSDATE yields the following error:
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
I'm re-creating a MATERIALIZED VIEW; which included some minor changes, and whenever the process aborts it always points to the '>=' in the following derived table query:
SELECT id,
desc,
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE
Now end_date is a type DATE, and I can actually execute this query by itself, but whenever I try to run it in the materialized view it always aborts with the error above. Although last week I was able to create it with the same query.
Any ideas?
Thank you,
Hi I'm terribly sorry for the long delay. I just couldn't post the whole statement for security reasons.
Now the issue has been resolved. The problem was that our materialized view script was aggregating data from 17 different places vía a UNIONs. Now for some reason the error was pointing to wrong line of code (see below).
SELECT id,
desc,
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE <-- ORACLE POINTS TO THIS LINE
Now this was like the tenth statement in the script, but the error really was in the sixth statement in the script; which was obviously misleading. In this statement a particular record (out of millions) was attempting the following operation:
to_date(' / 0/ ') <-- This was the cause of the problem.
Note that this text wasn't like this in the actual script it literally said to_date(<column name of type varchar>), but 2 records out of 15 million had the text specified above.
Now what I don't quite get is why Oracle points to the wrong line of code.
¿Is it an Oracle issue?
¿Is it a problem with the SQL Developer?
¿Could it be a conflict with a hint? We use several like this: /*+ PARALLEL (init 4) */
Thank you for all your help.
Is desc a column name? If yes then you are using a oracle reserved keyword desc as a column name.
SELECT id,
desc,---- here
start_date,
end_date
FROM T_LIPR_POLICY_ROLE TLPR
WHERE end_date >= SYSDATE
We cannot use oracle reserved keywords in column names.
Please change the column name.

Resources