I am trying to enter a date in oracle database using to_date in preparedStatement but I am getting errors.
Code Fragments:
sql = "select Identifier from metadata where content_cdate >=to_date(?,'dd-mm-yyyy') and content_cdate < to_date(?,'dd-mm-yyyy') and status='published' and content_mdate is null";
ps.setString(1, commonUtil.dateToString(startTime));
the dateToString method returns a value like this: 2012-01-01 12:00:00
Error:
[Oracle][ODBC][Ora]ORA-01861: literal does not match format string
Please advice.
The purpose of the TO_DATE function in Oracle is to convert a character value to a date value. In reality, it converts any value which has a data type of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 into a value which has the data type of DATE. It doesn't convert the value into any of the other datetime datatypes.
The TO_DATE function accepts an argument of a character data type and converts this value to a DATETIME value. The TO_DATE function evaluates a character string according to the date-formatting directive that you specify and returns a DATETIME value.
Prepared statements are much faster when you have to run the same statement multiple times, with different data. Thats because SQL will validate the query only once, whereas if you just use a statement it will validate the query each time.
You should use a correct TO_DATE
format mask to match your input.
In your case most likely: TO_DATE(?,'YYYY-MM-DD HH24:MI:SS')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With