1

I am trying to replace all the occurences of '-' in a column of a table. What I need is also to replace the string which exists after the dash and its a random number.

To be more specific this is one of my values:

"ANDRIU 5-9, CHAL 152 34, SOMETHING"

What I want is to replace this part:

-9

with an empty space. The problem is that: 9 can be any number and not necessarily one digit.

So I need something like finding the position of the first comma in the whole string. And the position of the dash and then replacing this based on the index values.

Is this possible?

1 Answer 1

2

Postgres provides the function regexp_replace(), which does what you want directly:

select regexp_replace(col, '-[0-9]+', ' ')
Sign up to request clarification or add additional context in comments.

2 Comments

Magic! For learning purposes: where does the + symbol help and where do you define to stop in the first comma?
@dkar . . . The documentation explains the regular expressions. But, [0-9] matches a single digit. The + matches one or more occurrences of the pattern (so multiple digits). So, the pattern stops at any non-digit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.