Using Postgres 9.6, dealing with millions of rows of data. How do I replace forwardslash '/' with a dash '-'? The problem is we have forward slash after a few more words in the string and that we want to keep it, so basically if there are more than 1 forwardslash then replace the first one with a dash and do nothing for the second one and if there is only one forwardslash then do nothing as well.
I have tried split_part()
and substring()
, but none of them worked. First, I want to know in a select query and then I want a second step to replace it. In the below code, column1
is varchar
column and that's where I want to make a change.
Data Example:
Current - someName/abc-def-tn/more-random-names
Expected - someName-abc-def-tn/more-random-names
Note: someName is not a of a fixed length, so cannot use positioning.
select column1,
reverse(split_part(reverse(column1), '/', 1)) as new_column_part1,
substring(column1, '[^/]*$') as new_url, as new_column_part2
from tableA