1

Maybe It is very simple for a person who are using database every day, but I need to write sql query. I've got fields (85500 items) which are in the same design for example '200 00 2334', '200 00 2555' (it's varchar data), it's a code of customers. I need to change all of this fields to for example '200-00 2334', '200-00 2555'. The first space in the field must be replaced into '-' the second space must stay without changes. My database is a PostgreSQL. I need only a query.

Thanks a lot for any help!

3 Answers 3

2

As chance would have it regexp_replace() will do the job with a trivial pattern-match since it will only match the first space.

SELECT regexp_replace('100 200 300', ' ', '-');
 regexp_replace 
----------------
 100-200 300
Sign up to request clarification or add additional context in comments.

1 Comment

that's what I want. I've used the query: update konta_wirtualne set kw_kod_kontrahenta = regexp_replace(kw_kod_kontrahenta, ' ', '-')
1
select code[1]||'-'||code[2]||' '||code[3] from
 (select regexp_split_to_array('200 00 2334', E'\\s+') as code) t

This keeps you more flexible if the first part of the code has different amount of charaters

Comments

1

Assuming it's always the 4th character, you can use overlay:

select overlay('200 00 12132' placing '-' from 4 for 1);
--------------------------------------------------------
OVERLAY
200-00 12132

Comments

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.