2

I am trying to update a table coulmn, the table has thousands of records.

enter image description here

Currently I am updating the table by running the following query manually for some of the records.

UPDATE MyTable
SET column = REPLACE(column, 'ABC', 'ABC9')
WHERE where   column like ‘ABC%’

Now I am trying to generate a generic query to update the table by adding a letter '9' after the alphabets. Thanks for your help

1 Answer 1

7

Use PATINDEX and STUFF

Patindex - Helps you to identify the first occurrence of numeric character in the string

Stuff - Helps you to insert 9 before the first occurrence of numeric character in the string

UPDATE MyTable
SET column = stuff(column,patindex('%[0-9]%',column),0,'9')
Sign up to request clarification or add additional context in comments.

2 Comments

You should also explain what it does, But +1 for the solution
@sagi - Thanks :) updated with MSDN links and some explanation

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.