Sql string replace

Posted on 2011-05-07
Last Modified: 2012-05-11
I would like to find and replace string in a column.
For example: TableA   MyColumn (varchar(50))
Replace USA with GB
Id=2   MyColumn: GER, USA, INDIA
Id=3   MyColumn: CHINA, INDIA, USA
The result should be:  
Id=2  MyColumn: GER, GB, INDIA
Id=3  MyColumn: CHINA, INDIA, GB
How is best to do this?
Question by:johnkainn
    LVL 9

    Accepted Solution

    You can use the Replace function in SQL to do this. It is the easiest and fastest way to do this,

    SELECT REPLACE(MyColumn,'USA', 'GB') FROM yourTableName
    LVL 12

    Assisted Solution

    The above solution works if you want to leave the data unchanged

    If you want to actually change the data, use:

    update TableA set MyColumn = REPLACE(MyColumn,'USA', 'GB')

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now