0

How do I do a WHERE COLUMN LIKE '%SearchTerm%' with the DevArt dotConnect for Oracle libraries?

Ideally using a CommandParameter for the SearchTerm?


I've tried the following with no joy:

cmd.CommandText =
  "SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
   WHERE COLUMN_A LIKE :SearchTerm";


I've got it working with:

cmd.CommandText =
  "SELECT ID, COLUMN_A, COLUMN_B FROM TABLE_A
   WHERE COLUMN_A LIKE :SearchTerm";

cmd.Parameters.AddWithValue("SearchTerm", "%" + term.Replace('%', ' ') + "%");


But I'm not happy with encasing the term with % signs - is there a correct or better way?

Andrew
  • 12,991
  • 15
  • 55
  • 85

2 Answers2

2

Oracle itself doesn't like "Where column LIKE %:searchterm%", so if you need the wildcards in there then they need to be added as part of the parameter.

You could avoid it by setting up a full text index on the column you want to search and then use CONTAINS instead, which doesn't require the wildcards. It's also a more powerful search method, but more complex to set up.

Community
  • 1
  • 1
Tridus
  • 5,021
  • 1
  • 19
  • 19
2

Try this solution:

cmd.CommandText =
  "SELECT ID, COLUMN_A, COLUMN_B
   FROM TABLE_A
   WHERE COLUMN_A LIKE '%' || :SearchTerm || '%'";

cmd.Parameters.AddWithValue("SearchTerm", term);
Andrew
  • 12,991
  • 15
  • 55
  • 85
Devart
  • 119,203
  • 23
  • 166
  • 186