slsy
slsy

Reputation: 1304

LIKE operator with a bindvar pattern which works also for special characters

I have such a query:

WHERE x LIKE $1

, where $1 is a bindvar string built in the backend:

$1 = "%" + PATTERN + "%"

Is it possible to build a LIKE PATTERN in that way that special characters (% and _) are escaped, so I have the same functionality, but it works for all possible PATTERN values.

Upvotes: 0

Views: 63

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

You would want to escape the literal % and _ with backslash. For example, in PHP we might try:

$pattern = "something _10%_ else";
$pattern = preg_replace("/([%_])/", "\\\\$1", $pattern);
echo $pattern;  // something \_10\%\_ else

Upvotes: 2

Related Questions