Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

sql - How to update Partial Value in Postgresql?

I have a table like below...

value        Name
-------------------
Data-1      Aarong
Data-125    Aarong
INT-121     Aarong
INT-122     Aarong

I would like to update value column with existing value. After update value column like below.

value       Name
-------------------
D-1        Aarong
D-125      Aarong
I-121      Aarong
I-122      Aarong

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the string always contain just one dash, you could do this with split_part():

update mytable set value = 
    left(value, 1) || '-' || split_part(value, '-', 2)

if there may be more than one dash, and you want everything after the first:

update mytable set value = 
    left(value, 1) || substr(value, position('-' in value))

Demo on DB Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...