0

I am using spring JDBCTemplate to query my data.


I have the following query in my code

static final String SQL = SELECT msgs.msg_submit_date MSG_DATE FROM {0}.messages msgs 
WHERE TRUNC(msgs.msg_submit_date) 
BETWEEN TO_DATE(:startDate,'YYYY-MM-DD') AND TO_DATE(:endDate,'YYYY-MM-DD') ;

String formattedSQL = MessageFormat.format(SQL , new Object[] "sms.schema"});

and I am getting an ORA-00904: "DD": invalid identifier error when I run it. I am assuming this is because the msg_submit_date column has HH:MI:SS associated with it, however, I don't want to place HH:MI:SS in my java code. Having year, month and day will suffice.

Also, when I run the same query, in my Oracle SQL Developer IDE, it runs fine without any errors so not sure what is going wrong.


The date gets generated via java.time.LocalDate using the following format:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/d/yyyy");

which returns the following string format 2018-06-03.

The jdbc call:

jdbcTemplate.query(formattedSQL, params, new MessagesRowMapper())

I think it maybe due to MessageFormat.

4
  • 1
    Show us your Java code. Edit your question - do not post code or additional information in comments. Commented Jul 12, 2018 at 18:04
  • @a_horse_with_no_name I have made the edit Commented Jul 12, 2018 at 18:14
  • The single quotes are being lost; is that really exactly how it looks, or is the entire string enclosed in double quotes? Does it need to be? (Also, why doesn't your ofPattern() match the to_date() pattern; and why are you passing strings instead of binding dates directly - doesn't Spring allow that?) Commented Jul 12, 2018 at 18:31
  • 1
    what about formattedSQL? How do you pass the values? Where is the code of the row mapper? Commented Jul 12, 2018 at 18:44

2 Answers 2

2

The error is because the query doesn't have the single quotes when it is executed; you can get the same thing with:

SELECT msgs.msg_submit_date MSG_DATE FROM messages msgs
WHERE TRUNC(msgs.msg_submit_date)
BETWEEN TO_DATE(:startDate,YYYY-MM-DD) AND TO_DATE(:endDate,YYYY-MM-DD);

ERROR at line 3:
ORA-00904: "DD": invalid identifier

The date format components are being seen as identifiers, not as part of a string.

You need to figure out why that is happening - the most likely explanation is that you don't have them in the static string in your Java code, despite what you have posted in the question, but they may be being stripped out by a later transformation, e.g. by the MessageFormat.format() call.

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

1 Comment

Ty. It was due to MessageFormat stripping out single quotes. Not sure why it does that.
0

You can write the query as:

SELECT msg_submit_date
FROM messages
WHERE submit_date >= DATE '2018-06-03' AND
      submit_date < DATE '2018-06-13';

Then you don't need to worry about any potential time component on the value.

That said, your problem appears to be with moving the data to your application. Perhaps this will help:

SELECT TRUNC(msg_submit_date)

Comments

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.