SQL - Update part of a field

I have a field which I need to updated but it has a pipe so not the entire field can be updated.

Example (using dummy data)
Camaro | Corvette
Camaro | Corvette
Camaro | Corvette
Camaro
Camaro | Corvette

I need to update Camaro with Acura using a SQL statement.

Thanks.
LVL 2
CipherISAsked:
Who is Participating?
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Without knowing any other requirements, use the REPLACE function to replace fragments of character values..
UPDATE YourTable
SET ColumnName = REPLACE(YourColumnName, 'Camaro', 'Acura') 

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
You can just use the replace:

Update <tbl>
set <field> = replace (<field>, 'Camaro', 'Acura')
-- where ?
0
 
Scott PletcherSenior DBACommented:
Here's how to insure that you only update the description to the left of a pipe, if a pipe exists:

UPDATE dbo.table_name
SET column_name = REPLACE(LEFT(column_name, CHARINDEX('|', column_name + '|')), 'Camaro', 'Acura') +
    SUBSTRING(column_name, CHARINDEX('|', column_name + '|') + 1, 8000)WHERE description LIKE '%Camaro%'
0
 
CipherISAuthor Commented:
Thank You, works great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.