MAK
MAK

Reputation: 7270

PostgreSQL: Replace string from specific position to end of string

I have the following string:

String 1: abcde?dafsdfdsfsd
String 2: absdfcde?dafsdfdsfsdsfdsdfd

Want to remove anything after "?"

Expected output:

String 1: abcde
String 2: absdfcde

Upvotes: 0

Views: 1034

Answers (1)

klin
klin

Reputation: 121889

Use split_part(), e.g.:

with my_data(col) as (
values
    ('abcde?dafsdfdsfsd'),
    ('absdfcde?dafsdfdsfsdsfdsdfd')
)

select split_part(col, '?', 1)
from my_data;

 split_part 
------------
 abcde
 absdfcde
(2 rows)    

Upvotes: 3

Related Questions