0

I having a table like this

---------------------------
|id |condition_values     |
---------------------------
|1  |2012-05-12           |
|2  |2012-06-12           |
|3  |2012-07-12           |
|4  |2012-08-12           |
---------------------------

So when I trying to query like

select * from tableName where condition_values >= TO_DATE('2012-05-12', 'yyyy-mm-dd')
AND condition_values <= TO_DATE('2012-07-12','yyyy-mm-dd');

I getting SQL Error

ORA-01861: literal does not match format string

Note: value is a VARCHAR2 field

2 Answers 2

3
select * from tableName where "value" >= TO_DATE('2012-05-12', 'yyyy-mm-dd') AND "value" <= TO_DATE('2012-07-12','yyyy-mm-dd');
--                             ^^^^^

You have an implicit type conversion here using you NLS date format, as, according to the documentation (same link):

When comparing a character value with a DATE value, Oracle converts the character data to DATE.


The quick fix is to write:

select * from tableName where TO_DATE("value", 'yyyy-mm-dd') >= TO_DATE('2012-05-12', 'yyyy-mm-dd') 
                           AND TO_DATE("value", 'yyyy-mm-dd') <= TO_DATE('2012-07-12','yyyy-mm-dd');

But, as your date format is lexicography comparable, you might write that instead:

select * from tableName where "value" >= '2012-05-12' 
                           AND "value" <= '2012-07-12';

However, I would strongly suggest you to fix your data to ensure that you use the proper DATE type for your column. This will avoid a bunch of possible bugs and problems like this one.

As suggested by Falco in a comment below, "if you cannot change the column type to DATE, you should probably create a function-based Index for the TO_DATE(...) Value to get fast query times".

Sign up to request clarification or add additional context in comments.

4 Comments

If you cannot change the column type to date, you should probably create a function-based Index for the TO_DATE(...) Value to get fast query times! - you could then use the index with a view - so no extra coding is required. But saving the initial data as DATE would be the best option
Thank you for your comment, @Falco ! I took the liberty to add that to my answer. Please fell free to revert if you desagree.
@Falco Sometimes in the same column normal string values would also come
@Suganthan Then you should create a function based index which returns NULL for ROWs which do not contain a valid date. See also: stackoverflow.com/questions/5966274/…
1

VALUE is a reserved word. Either change the column name or:

select * from tableName 
where `value` >= TO_DATE('2012-05-12', 'yyyy-mm-dd')
AND value <= TO_DATE('2012-07-12','yyyy-mm-dd');

Reference: Reserved Words in Oracle

If I were you, I will avoid using reserved words in column names.


OP has updated the question. It is recommended to use DATE column type instead of VARCHAR2 to avoid TO_DATE() conversion. Faster querying, Less problem.

1 Comment

Sorry. the column name is not value that is just a test column name. I 'll update my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.