1

The following sql when run with these parameters,

:P_COMP_DATE_FROM = '15-NOV-2015' :P_COMP_DATE_TO = '15-NOV-2015'

compares as between '15-NOV-2015 00:00:00' and '15-NOV-2015 00:00:00'

Select Ordered_date
From xxcost_rep
Where DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(:P_COMP_DATE_TO)), DATE_COMPLETED);

how can I compare this as start of the day and end of the day, so can display the correct result in the range.

I am trying the following to add 86399 seconds to make it the end of the day, but receiving error:

WHERE DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(to_date(:P_COMP_DATE_TO,'DD-MON-YYYY')+interval '86399' second), DATE_COMPLETED)

{P_TO_CUSTOMER=, P_COMP_DATE_FROM=2015/11/15 00:00:00, P_COMP_DATE_TO=2015/11/15 00:00:00, P_TO_ORDER_NUMBER=, P_CUST_REGION=, P_TO_DATE=, P_JOB_STATUS=, P_FROM_DATE=, P_FROM_ORDER_NUMBER=, P_FROM_CUSTOMER=} Calling XDO Data Engine... --SQLException java.sql.SQLDataException: ORA-01861: literal does not match format string

Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
Muhammad Nadeem
  • 23
  • 1
  • 10
  • Post code of `fnd_date.canonical_to_date`. The error is due to the fact that you are depending on your local **NLS** date format which is different from what you are using. you must always explicitly provide the **format mask**. – Lalit Kumar B Nov 18 '15 at 08:23
  • Don't pass dates as strings. Use proper `date` variables. Then you also don't need that workaround using your own function to convert a string to a date. –  Nov 18 '15 at 08:50

1 Answers1

1

ORA-01861: literal does not match format string

The above error is because the date literal doesn't match with the format mask.

For example,

SQL> SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual;
SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual
               *
ERROR at line 1:
ORA-01861: literal does not match format string

You might be storing dates as string, and there might be strings with different date formats. Therefore, your function fnd_date.canonical_to_date might be failing for such date literals while converting into DATE using TO_DATE.

Also, you should not depend on your client's NLS date format. Remember, TO_DATE is NLS dependent. You should explicitly mention the format mask.

For example,

SQL> SELECT  to_date('11/18/2015 00:00:00', 'mm/dd/yyyy hh24:mi:ss') date_from,
  2          to_date('11/18/2015 23:59:59', 'mm/dd/yyyy hh24:mi:ss') date_to
  3  FROM dual;

DATE_FROM           DATE_TO
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59

In your case, you need to compare the dates. You could do it like the below example,

SQL> WITH DATA AS(
  2  SELECT DATE '2015-11-18' dt FROM dual
  3  )
  4  SELECT * FROM DATA
  5  WHERE dt
  6  BETWEEN to_date(
  7                  to_char(dt, 'mm/dd/yyyy')||' 00:00:00',
  8                  'mm/dd/yyyy hh24:mi:ss'
  9                 )
 10  AND     to_date(
 11                  to_char(dt, 'mm/dd/yyyy')||' 23:59:59',
 12                  'mm/dd/yyyy hh24:mi:ss'
 13                 );

DT
-------------------
11/18/2015 00:00:00

UPDATE

For the first part where you just need the start time, you don't have to add the time portion as 00:00:00since DATE has both date and time elements. When you do not mention the time portion, it defaults to midnight i.e. 00:00:00.

For example, add INTERVAL '86399' SECOND:

SQL> SELECT DATE '2015-11-18' from_date,
  2         DATE '2015-11-18' + INTERVAL '86399' SECOND to_date
  3  FROM dual;

FROM_DATE           TO_DATE
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124
  • Removed fnd_date.canonical_to_date function. Now +interval '86399' second is working for one parameter but not working for another. – Muhammad Nadeem Nov 18 '15 at 12:04
  • Not working is not an oracle standard error. You need to edit your question and post the error. Why would it work for one parameter and not the other? Compare and see what is different. For the other parameter why do you even need to add an interval? You need it to start at time `00:00:00` So leave the date portion, it will have the time portion as `00:00:00`. – Lalit Kumar B Nov 18 '15 at 12:58
  • I need to pick the values for between '15-NOV-2015' and '15-NOV-2015', but it ignore the records which have date like '15-NOV-2015 11:33:00'. because it compare between '15-NOV-2015 00:00:00' and '15-NOV-2015 00:00:00'. for this purpose I used [:P_COMP_DATE_TO+interval '86399' second ] so it can compare like this '15-NOV-2015 00:00:00' and '15-NOV-2015 23:59:59' – Muhammad Nadeem Nov 19 '15 at 06:00
  • @MuhammadNadeem I gave a complete answer with two working examples. What is the problem using that? – Lalit Kumar B Nov 19 '15 at 06:05
  • Why date between '15-NOV-2015' and '15-NOV-2015', not picking the record with '15-NOV-2015'? because it has time portion after 00:00:00. Now how can I solve the problem. I tried :P_COMP_DATE_TO+interval '86399' second. Is this a right approach? or some way other can be possible? – Muhammad Nadeem Nov 19 '15 at 07:49
  • @MuhammadNadeem I have added an example in the update as per your request. – Lalit Kumar B Nov 19 '15 at 08:10