Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using oracle's to_date in preparedStatement

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.

like image 809
Nelo Angelo Avatar asked Apr 12 '12 08:04

Nelo Angelo


People also ask

How does TO_DATE work in Oracle?

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.

What is the use of TO_DATE function?

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.

Is PreparedStatement faster?

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.


1 Answers

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')

like image 197
Anonymous Avatar answered Sep 18 '22 06:09

Anonymous