I am looking for a simple way to search in a database for all rows where a particular column has a case insensitive sub-string match to a search string.

Google told me to use the LOWER() SQL function, so I wrote some literal SQL like this:

SELECT * FROM tblItem me where( lower(me.name) LIKE '%example%' )

It works nicey, however I am having trouble finding a neat way to translate it into a DBIx::Class::ResultSet search term.

Google searches found some posts on the DBIx-Class mailing list that said that I should write:

my $searched_rs = $all_items->search({'LOWER(me.name)' => {'LIKE'=>'%example%'})

However this did not work. I got an error: DBI Exception: DBD::mysql::st execute failed: Unknown column 'LOWER(me.name)'

From reading the FAQ, it said that the only way to use functions on the left side of a search term is to pass the whole query as literal SQL. That gives me:

my $searched_rs = $all_items->search( \[ 'LOWER(me.name) LIKE ?',[ plain_value => "%example%"] ] );

That works, but it is ugly, does not look to be very portable, feels against the spirit of using SQL::Abstract to insulate the perl program from the different database dialects of SQL.

Is there a neater way to do case insensitive sub-string searches? I am using MySQL and SQLite BTW.


In reply to DBIx::Class case insensitve substring search. by chrestomanci

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.