Ion C
Ion C

Reputation: 323

Postgresql replace function

When I do a select like this

select
replace(replace(t.field,'{0}',t.parameter01), '{1}', t.parameter02)
from table t

I obtain a null result even if t.field contain text without any placeholders. Can somebody explain why this happens?

Upvotes: 1

Views: 273

Answers (1)

Ion C
Ion C

Reputation: 323

I already figured out. If the third parameter of replace function is null, the result will be null although the second one doesn't match anything in the first one. An workaround is to replace the value of third parameter if it is null:

select 
replace(t.field, t.parameter01, coalesce(t.parameter02, ''))
from table t

Upvotes: 1

Related Questions