I need to remove only character 1 from the string ‘[1,11, 1, 111,12,1,11]’ in Postgres without affecting the other characters. Symbol 11 and 111, 12 should not be affected. I can’t cope with this task. Please help me
The end result should be the string ‘[11,111,12,11]’
I tried different replace options REPLACE(%s,’ 1,’,”), REPLACE(%s,’1 ,’,”)
2
Answers
Use
substring(string [from <str_pos>] [for <ext_char>])
.Inside out: first replace
[
and]
with{
and}
for correct array syntax, then cast the resulting string to a text array andunnest
it into a table of strings. Select elements that are not equal to ‘1’, aggregate the resultset back into a string and format it within square brackets.DB-Fiddle