0

First of all - all credits goes to Laurenz Albe answer here

I hava a JSONB colum where I need to perform a search on the values (exactly as explained in the linked question). I think to get advantage from the answer there and the trigram index. However in the answer the regex which is searched is hardcoded directly WHERE visitors::text *~ '\mJohn Doe\M';

My value comes from the front end where the user enters a search string which is named in the postgres function as search_term.

If I was to use the answer directly I would have to execute WHERE visitors::text search_term which obviously won't work.

My question is how to continue the query so that I can modify the search_term variable and get the same effet as the *~ '\mJohn Doe\M' in the original post?

GMB
  • 195,563
  • 23
  • 62
  • 110
Leron
  • 9,244
  • 34
  • 149
  • 249

1 Answers1

2

Just pass the variable as the right operand to regexp operator *~;

WHERE visitors::text *~ '\m' || search_term || '\M'
GMB
  • 195,563
  • 23
  • 62
  • 110