Hello all.

I'm attempting to pre-build part of a SQL statement in perl before passing it to the DBI engine. My SQL statement needs to do a like in the DB engine, and I'd intend to utilize the SQL % sign. However, the presence of the % sign in the string has perl attempt to satisfy the %B or %1 as the string gets assigned instead of just passing it along as a string.

I'd like my SQL statement to eventually resemble the following:

select winner from results where round='2' and (event like '%USA%' and + event like '%Boys%');

here's a code snippet example of the perl I'm attempting to write:

if ($form{'category'} eq 'All USA Boys') { $clause="(event like '%USA%' and event like '%Boys%')"; } printf "Existing clause assignment logic; clause variable is $clause<p +>" if $debug;

The printf of the above code ends up as follows:
Existing clause assignment logic; clause variable is (event like '0SA%' and event like '0oys%')

%U got turned into 0, as did %B. This also happens with a few other words where I attempt to wrap the word in a like statement.

How do I get Perl to NOT interpret this and to pass the string along as intended?

I've tried escaping the % sign with a \, tried \Q and \E, tried replacing the % sign as a variable, tried breaking up the strings at the % signs and then re-concatenating them, etc. All to no avail.

Hoping i'm missing something simple...thanks in advance.


In reply to Escaping % sign in a like string bound for SQL by bfr7kq6

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.