Americo
Americo

Reputation: 919

How Do I Trim A String When It Reaches A Certain Character (Parenthesis)

enter image description here

I have this column with a string inside of it. I want to return a result that shows the provider column with all characters inside of it up, but not including, the space before the first open parenthesis.

What is the best strategy for accomplishing this?

i.e. turn "NEAL K HANSEN (4406570001)" to "NEAL K HANSEN" and so on.

Upvotes: 0

Views: 89

Answers (1)

Kusalananda
Kusalananda

Reputation: 15603

Use regexp_replace(), like this:

regexp_replace(provider, ' \([^)]*\)', '')

For example:

=> select regexp_replace('"NEAL K HANSEN (4406570001)"', ' \([^)]*\)', '');
regexp_replace
-----------------
"NEAL K HANSEN"
(1 row)

The function call replaces everything matching "a space, an open paren, any number of characters that is not a closing paren, and a closing paren" with nothing (an empty string).

Upvotes: 1

Related Questions