3

I have data in a table like this:

xxx-xxxxxxx-29
xxx-xxxxxx-87
xxx-xxxxxxxxx-2
xxx-xxx-33

I need to replace every -n at the end with -0n (to add a zero between the dash and the number). Leave -nn as it is.

So, if 2nd character from the right is '-' replace it with '-0'

can this be done using UPDATE?

2 Answers 2

3

I need to replace every -n at the end with -0n (to add a zero between the dash and the number).

One option is to use regexp_replace():

update mytable set mycol = regexp_replace(mycol, '-(\d)$', '-0\1');

'-(\d)$' matches on a dash followed by a one digit at the end of the string, and captures the digit; you can then prepend a leading '0'.

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

1 Comment

@GMB '-(\d)$' is good one +1. On a large data set, a where condition would be more efficient in performance.
2

Here is one way:

update mytable
set data = substr(data, 1, length(data)-1) || '0' || substr(data,-1)
where data like '%-_';

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.