0

I have a table in a db that is labeled like so:

TableA:

  ID               ID2    Text
0 64556-546-1    465846    adfadf    
1 64556-546-1    465846    adfadf
2 64556-546-1    465846    adfadf

I want to trim the symbol(-) and digit after the dash only from the first column and only the dash in the second position how would I go about this? so the number will look like 64556-546.

The value will be compared to another in another table, so no need to create another column.

Thank you in advance.

1 Answer 1

2

I'd check here;

https://www.postgresql.org/docs/9.1/static/functions-string.html

and do a;

regexp_replace(string text, pattern text, replacement text [, flags text])

So one way to do it (ignoring probable performance issues), would be;

SELECT TRIM(BOTH '-1' FROM regexp_replace(ID, '[-]', '' )) FROM TableA

But you could probably build a better way with a more advanced regex.

Sign up to request clarification or add additional context in comments.

2 Comments

Hint: select substring('64556-546-1' from '[^-]+-[^-]+'); ;)
@Abelisto thank you for the helpful hint! and thank you juju for the reference!

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.