Home Forums SQL Server 2005 SQL Server Express How to replace multiple items in a column in sql server express table RE: How to replace multiple items in a column in sql server express table

  • Jeff Moden (4/21/2016)


    ironryon (4/21/2016)


    GilaMonster (4/21/2016)


    Oh, so what, in the spreadsheet tells you what row in the table the new email address belongs to?

    Hi GilaMonster

    No it wont tell me what row. What the spreadsheet will have is the username and the new email address....

    So it would be like this

    username email address

    J1234 John.Smith@gmail.com

    D1234 David.Jones@gmail.com

    .....

    In the table where I need to update the email address column there is also a username column..

    According to your original post, you already have the data in the table, so we don't actually need to worry about the spreadsheet. Just what's in the table.

    I don't know what your table name is or the datatypes but, if all you want to do is change every occurrence of "@Hotmail.com" to "@GMail.com", the substitute the correct schema name and table name into the following code and it should do fine. You included a space in the "email address" column, which is why it's included in brackets. You may have to change that as well.

    Once you made the necessary changes to the following code, run it.

    BEGIN TRANSACTION

    UPDATE dbo.yourtablenamehere

    SET = REPLACE(,'@Hotmail.com','@GMail.com')

    WHERE LIKE '%@Hotmail.com%'

    ;

    -- ROLLBACK -- COMMIT

    IF the returned rowcount is what you expected, then double-click on COMMIT to select it and run just the COMMIT code to make it permanent.

    If the returned rowcount is NOT what you expected, then double-click on ROLLBACK to select it and run just the ROLLBACK code to undo the update and then figure out what went wrong.

    As a bit of a sidebar, some people like to run their UPDATE code as a SELECT first as a bit of a test. I usually do the same but, either way, I always use the BEGIN TRANSACTION and the commented out ROLLBACK and COMMIT to help keep accidents from happening. Never walk away from an active transaction because you could end up blocking the database up pretty good.

    My next suggestion, since you're a newbie, would be to spend some time studying SQL Server and T-SQL (the language).

    Hi Jeff

    Not all the users in the table will have their email addresses updated to the example @gmail.com.

    There are approx 7000 users/rows in this table with a variety of email address values.

    So only 300 of the 7000 will need to be updated with a new email address. Two of the columns in the table are for "username" and "email address".

    The spreadsheet I have has 300 usernames and email addresses.

    Thanks for your suggestion - I definitely need to study a lot 🙂