0

I have a query like this..

String query = "UPDATE tbl_customer_policies SET "+
        "start_date = ?," + 
        "next_pay_date = ?,"+
        "maturity_date = ?, " + 
        "modified_at = CURRENT_TIMESTAMP,"+
        "modifier = ?, status = ? " + 
        "WHERE id = ?";

Now in place of the place-holder for start_date I want to pass a string like SYSDATE.

What I am doing now is setting that string in a variable called String startDate = "SYSDATE" and binding it to that place-holder. But I guess it does not seem to work. I get this exception

ORA-01858: a non-numeric character was found where a numeric was expected

I have to pass Oracle functions like that. How to achieve that?

2
  • 3
    AFAIK, you can't. It must be in the query itself, and not in its parameters. Commented Mar 10, 2013 at 8:44
  • actually i want the db itself to handle date calculations. I am not processing that in java..was trying to execute that logic using db functions only like.. ADD_MONTHS(SYSDATE, 12). With that i didn't have to do that date processing in java. Commented Mar 10, 2013 at 8:52

2 Answers 2

2

If it will always be sysdate you don't need to parameterize it. Set it directly in the query like this:

String query = "UPDATE tbl_customer_policies SET "+
        "start_date = sysdate," + // or "start_date = ADD_MONTHS(SYSDATE, 12),"
        "next_pay_date = ?,"+
        "maturity_date = ?, " + 
        "modified_at = CURRENT_TIMESTAMP,"+
        "modifier = ?, status = ? " + 
        "WHERE id = ?";

And set all the others parameters like you did before.

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

2 Comments

it will not be sysdate always. Based on conditions it can change.
So either you have a set of SQL query that you can change from, or you could maybe crete a stored procedure that can be called from your sql query, but it seems a little overkill..
0

Use function TO_DATE (docs) for example

    "UPDATE tbl_customer_policies SET "+
     "start_date = TO_DATE(?, 'DD-MON-RR')," + 
    "next_pay_date = TO_DATE(?, 'DD-MON-RR'),"+

and don't forget pass parameters in format like '27-OCT-98' in this case.

P.S. I misunderstood the question so try to correct my answer.

The described problem is for limitations of PreparedStatement Java class. PreparedStatement object is used for storing a reference to some precompiled (and optimized) SQL statement.
So you have to supply only values of parameters for filling them. Functions must be evaluated before using their results and they aren't able to be placed there.

I guess you can use a workaround with Oracle Structured Types. You can pass them as reference types with PreparedStatement's setRef() method. Using functions can be implemented with a wrapper in constructor of object. I didn't try it but it seems to me it is a possible solution.

2 Comments

Thats not the answer to my question. Read it again. I know I can pass it using TO_DATE. I am asking whether i can use that function as a string, store it in a variable and pass it.
@pjp I'm sorry - I understood that you can't decide the problem with the error ORA-01858. See my second attempt :)

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.