0

I have a problem with the following:

I have a sequence that comes with the value:

"ORA-00904: TESTE: identificador inválido"

I need to replace this test field to empty ( '') in my query .

But if my query contains other field with the name for example TESTETE and I replace for '', it staies wrong, replacing TESTE for and TESTETE forTE

I want to replace the TESTE for `` and the TESTETE let how it is.


my example query is :

SELECT TESTE, TESTETE, fld1 FROM TBL


My logic is as follows:

String oracleMsg = "ORA-00904: TESTE: identificador inválido";
String query = "SELECT TESTE, TESTETE, OUTRO FROM TBL";
String comp = "TESTE";

if (oracleMsg.contains(comp)){
    query = query.replace(comp, "''");
}
System.out.println(query);

Result: SELECT '', ''TE, OUTRO FROM TBL Expected Result? SELECT '', TESTETE, OUTRO FROM TBL

Thank you a lot!!!

2
  • you can expand your comp var to "TESTE," and replace it with '', but this will rise an issue when the field you replace is the last one (the missing ',' at the end of the field list) Commented Apr 15, 2016 at 13:34
  • I Can`t do it friend. Because "TESTE" comes dynamically in my system. What i posted here was just an examplo. Other sugestions ??? Commented Apr 15, 2016 at 13:38

1 Answer 1

1

Use regular expressions and surround your pattern with word-boundary anchors.

 query = query.replaceAll("\\b"+comp+"\\b", "''");

In case you want to replace only one instance of the pattern, use replaceFirst instead of replaceAll.

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

1 Comment

Hello Joni, your way is good. I Could fix my problem with you solution. Thank you!

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.