5

I am using to write a select query and the value for like statement is dynamic.

  AND       e.rank_request_id = a.request_id
  AND       f.priority_request_id = a.request_id
  AND       b.status_type_id = c.status_id
  AND   b.status_request_id = a.request_id
  AND   a.request_id LIKE '%#form.searchbar#%'

But this returns results only where Case of each character in the string #form.searchbar# is matched.

Please suggest a workaround for this so that it becomes case-insensitive.

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
aman.kejriwal
  • 225
  • 3
  • 6
  • 13

4 Answers4

18

I do not know what database you are using but if this were for Oracle then you could just force the case of both things. This though comes at a cost for execution times since it does it for all values in that column but you'd only see the cost if you have a lot of data and could work around that with a function based index. So something like this, again for Oracle:

AND UPPER(a.request_id) LIKE '%#UCase(Form.Searchbar)#%'

But I would suggest you use a queryparam since appears to come from a user inputted box, so:

AND UPPER(a.request_id) LIKE <cfqueryparam value="%#UCase(Form.Searchbar)#%" cfsqltype="cf_sql_varchar" />
Snipe656
  • 845
  • 7
  • 15
  • If you use upper in a column, the database will not use an index if defined in that column, son in a big table this will run very slow. You can define an index with the upper function and leave the query like the example and then the database will use the index. Always review the execution plan for the decision made by the database to use or not your index. – Juan Oct 06 '14 at 01:22
  • True, if it is a large table running any function on the column like my example will be slow. Table size and slowness dependent upon resources/setup of course. I just wanted to add to the suggestion in case the original poster runs into this or someone else, do a search on Function Based Indexing. – Snipe656 Jul 29 '19 at 17:13
3

You could lower a.request_id and form.searchbar

AND lower(a.request_id) LIKE '%#form.searchbar#%'
Soader03
  • 351
  • 4
  • 20
2

There are already many questions about case-insensitive where clauses in Oracle:

For example, here.

Also, this query looks like it may be vulnerable to SQL injection attacks. More info here.

Community
  • 1
  • 1
Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
1

You can force everything to uppercase as Snipe656 suggests. You can also use the regexp_instr function to do a case-insensitive search. For example, to search the EMP table for every row where ENAME contains the string 'in' in a case-insensitive fashion

SQL> ed
Wrote file afiedt.buf

  1  select ename, empno
  2    from emp
  3*  where regexp_instr( ename, 'in', 1, 1, 1, 'i' ) > 0
SQL> /

ENAME           EMPNO
---------- ----------
MARTIN           7654
KING             7839

In your case, it would probably be something like

AND   regexp_instr( a.request_id, '#form.searchbar#', 1, 1, 1, 'i' ) > 0
Justin Cave
  • 227,342
  • 24
  • 367
  • 384