# Oracle SQL query & date condition

**URL:** <https://forum.knime.com/t/oracle-sql-query-date-condition/40337>\
**Category:** KNIME Extensions\
**Created:** [March 2, 2022, 10:39am UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337 "2022-03-02T10:39:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AreQ](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@AreQ](https://forum.knime.com/u/AreQ)\
**Post date:** [March 2, 2022, 10:39am UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337/1 "2022-03-02T10:39:33Z")

</div>

Hi,  
I am new in Knime. I started to built workflow which is using Oracle database. I used “Oracle connector” node, then I am trying to use “DB querry reader” node with built-in SQL statement. Statement is working fine, but when I add condition relating the date field then it shows me error “ORA-01861: literal does not match format string”.  
Any Idea?

Simple example of SQL statement below:

**Select contract.id, contract.activdate**  
**from contract**  
**where**  
**contract.activdate\>'2022-01-01’**

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [March 3, 2022, 8:32am UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337/2 "2022-03-03T08:32:18Z")

</div>

> [@AreQ](#):
>
> ORA-01861: literal does not match format string

Hi @AreQ , this error is likely to occur when you supply a literal date string to Oracle that is not in the format it is expecting.

I assume that the `activdate` column on your `contract` table is an oracle DATE datatype.

Oracle requires that if you pass a date as a literal without using to `TO_DATE` function (to state the format of the date you are supplying), that the format matches the `NLS_DATE_FORMAT` for your current session. Assuming you haven’t specified NLS\_DATE\_FORMAT, it will probably be DD-MON-YY e.g. ‘01-JAN-22’.

If you want to, you can find out what the current expected format for a date is by simply returning the current sysdate from oracle,

e.g. using `SELECT SYSDATE FROM DUAL`

As the date format could be modified for a given session,( using `ALTER SESSION SET NLS_DATE_FORMAT = '<date format>'` ), it is best with Oracle to ALWAYS wrap date literals with the TO\_DATE function, so that you are making no assumptions about the current default format.

Assuming that the date literal you have is in the format YYYY-MM-DD, you should find that changing your query to the following should work:

```auto
Select contract.id, contract.activdate
from contract
where
contract.activdate>TO_DATE('2022-01-01’,'YYYY-MM-DD')

```

Please note that if there is a time component to your `activdate` column (so that the column contains a time other than midnight, and you only wish to find dates that are AFTER 2022-01-01, then you will also need to take steps to “truncate” the date, which means it will return the date with the time set to midnight.

e.g.  
`where TRUNC(contract.activdate)>TO_DATE('2022-01-01’,'YYYY-MM-DD')`

This would have an implication on indexing though, if there is an index on `contract.activdate`, so if that is a problem you may have to look into ways to deal with this such as “functional indexes”.

As I mentioned, this is only a problem though if your activdate contains time as well.

For example, if contract.activdate contains the date/time ‘2022-01-01 23:32:00’, then it will be returned even though the actual date part is not greater than ‘2022-01-01’. This is because in Oracle it would be comparing ‘2022-01-01 23:32:00’ with ‘2022-01-01 00:00:00’ which you can see is “greater”.

By putting TRUNC around the date, the condition would then translate to `'2022-01-01 00:00:00' > '2022-01-01 00:00:00'` which then means that the row would NOT be returned, and this is likely to be what you had in mind.

---

<div class="post-metadata">

**Author:** ![AreQ](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@AreQ](https://forum.knime.com/u/AreQ)\
**Post date:** [March 3, 2022, 11:04am UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337/3 "2022-03-03T11:04:12Z")

</div>

Many Thanks takbb.  
“TO\_Date” is working fine. Such simple solution and so effective! 🙂

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [March 3, 2022, 1:48pm UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337/4 "2022-03-03T13:48:29Z")

</div>

You’re welcome @Areq. Glad it worked for you, and welcome to the KNIME community!

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [March 10, 2022, 1:48pm UTC](https://forum.knime.com/t/oracle-sql-query-date-condition/40337/5 "2022-03-10T13:48:52Z")

</div>

Closed on March 10, 2022, 1:48pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
