ttham ttham - 5 months ago 8
SQL Question

SQL server: How to update rows with additional information

I have a situation in which a table has information like:

First Name | Last Name | Email
John Doe jd@email.com
Jane Dont jnd@email.com


And I have a user who wants their email added on to both row's emails to looks like:

First Name | Last Name | Email
John Doe jd@email.com;a@email.com
Jane Dont jnd@email.com;a@email.com


Thank you in advance for any help.

Answer

To add a@email.com to all rows in your table:

UPDATE Table
SET Email = Email + ';a@email.com'

To update certain rows:

UPDATE t
SET t.Email = Email + ';a@email.com'
FROM Table t
WHERE t.FirstName = 'John'

Note: the above query will update all records with the first name of John