2

Suppose you have a query like this:

SELECT * FROM the_table WHERE (name LIKE %$1%) LIMIT 10

And a values array like this:

[tyrone]

The query works without the %% syntax used with LIKE operator. Is it possible to combine these ideas? Any reference to the LIKE operator I can find abandons the use of placeholders.

I am receiving the following error when attempting the query above:

{"name":"error","length":90,"severity":"ERROR","code":"42601","position":"38","file":"scan.l","line":"1086","routine":"scanner_yyerror"}

Michael Fulton
  • 4,608
  • 3
  • 25
  • 41

1 Answers1

2

SQL doesn't have string interpolation so you cannot use placeholders in string literals. You must instead use concatenation, e.g.

SELECT * FROM the_table WHERE (name LIKE '%' || $1 || '%') LIMIT 10

Note that the result of '%' || NULL is NULL.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778