dsb has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to find out if a SQL wildcard ('%') can be used in a DBI script next to a placeholder. Something like this:
$sth = $dbh->prepare( "SELECT name FROM people WHERE name LIKE ?%" );
Anyone know of something like that? I am trying to avoid having to make the wildcard part of the variable.

Replies are listed 'Best First'.
Re: DBI Question
by jreades (Friar) on Jan 17, 2001 at 03:08 UTC

    You can do this:

    $sth = $dbh->prepare("SELECT name FROM people WHERE name LIKE ?"); $sth->execute('%foo%');

    Just, remember, as I said in chatterbox, to escape the wildcard character '%'.

      The value I will bind to the placeholder is SCALAR variable. Can this still be done like:
      $sth = $dbh->prepare("SELECT name FROM people WHERE name LIKE ?"); $sth->execute( "$foo\%" );

        You'll have to test it for yourself, but I don't see why not...

        An easier way, if you know that you're always doing wildcard searches, might be:

        $sth->execute('%' . $foo . '%');
Re: DBI Question
by cadfael (Friar) on Jan 17, 2001 at 20:59 UTC
    I am trying to find out if a SQL wildcard ('%') can be used in a DBI script next to a placeholder. Something like this: $sth = $dbh->prepare( "SELECT name FROM people WHERE name LIKE ?%" ); I do this on a regular basis without needing to escape the '%' character, but in Sybase (and I assume MS SQL Server) , a string with a wildcard needs to be quoted:

    $sth = $dbh->prepare( "SELECT name FROM people WHERE name LIKE 'Jones%'" );

    -----
    "Computeri non cogitant, ergo non sunt"

      Yes. Now how do i do it with a place holder and scalar variable.
      $sth = $dbh->prepare( "SELECT name FROM people WHERE name like ?" ); $sth->execute( $name );
      how do include a wildcard in the where the placeholder is without appending it to the variable.