C47_C0D3R
C47_C0D3R

Reputation: 53

How to use the Postgres FORMAT function along with LIKE patterns that contain %?

I am trying to create a formatted string for a WHERE clause. I need to insert LIKE syntax inside the FORMAT() function.

Here is my SQL code snippet:

FORMAT(' AND student_name LIKE  %%L% ', CAST(jsnData->>'term' AS VARCHAR) );

Upvotes: 0

Views: 1421

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247790

As the documentation says,

In addition to the format specifiers described above, the special sequence %% may be used to output a literal % character.

So your expression should be

format($$ AND student_name LIKE  '%%' || %L || '%%' $$, jsnData->>'term')

(The cast is unnecessary.)

Upvotes: 2

Related Questions