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:
//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();