S. Schenk
S. Schenk

Reputation: 2150

Using variables inside a string in plpgsql

I'm trying to build a regex match with variables as part of the matched string:

var_a := 'somestring'
var_b := 'someotherstring'

IF EXISTS (SELECT 1 FROM some_table WHERE some_field ~* 'var_a.+?(?=\-)\-var_b)')

How do I insert the variables into the string? Like in js you can simply do:

`${var_a} restofstring`

Upvotes: 0

Views: 231

Answers (1)

user330315
user330315

Reputation:

You could use the format() function:

WHERE some_field ~* format('%s.+?(?=\-)\-%s)', var_a, var_b)

Upvotes: 1

Related Questions