Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ORA-00932: inconsistent datatypes: expected TIMESTAMP got CHAR

I have the below query excerpt which is part of a very large query and looks like this:

SELECT to_timestamp(TO_CHAR(CAST(coalesce(?, '01-JAN-01 01:01:01.000000 AM') AS TIMESTAMP), 'DD-MON-YY HH24MISS')

Now, I am trying to pass value to above coalesce function through my java code like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//query.setParameter(1, "01-JAN-01 01:01:01.000000 AM"); // It works, hardcoded
query.setParameter(1, last_run_date, TemporalType.TIMESTAMP); // gives error as in the title
results = query.getResultList();

I am getting last_run_date (java.sql.Timestamp) from another table which looks like this: 2023-05-08 22:18:25.112

How can I convert last_run_date parameter to the desired format i.e 01-JAN-01 01:01:01.000000 AM
I need it to be done through Java code. Can someone point me to the right direction?

>Solution :

Instead of formatting the timestamp in the SQL query, you could format it in your Java code before setting the parameter, like:

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSS000 a");
String formattedDate = sdf.format(last_run_date);

query.setParameter(1, formattedDate);
results = query.getResultList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading