1

I need to update part of the string in SQL Server.

I have:

ID   IDCityVisited   Names                    
1    10              Julya,Matheus,Donovan    
2    15              Mary,Donovan,Richard
3    20              John,Bob,Andy

I need to update the Names column changing only Donovan for Paul and keeping all the remaining string as it is.

Thanks

1
  • This has already been asked and should be fairly easy to answer yourself with reference to Google and MSDN. What have you tried and what precisely is your difficulty? Commented Feb 15, 2013 at 17:10

5 Answers 5

6

As @Pondlife point me, this worked:

UPDATE myTable
SET Names= REPLACE(Names, 'Donovan', 'Paul')
WHERE Names LIKE '%Donovan%'

Thanks All

Sign up to request clarification or add additional context in comments.

Comments

4

You need to update your table

UPDATE tblName
SET Names = REPLACE(Names,'Donovan','Paul')

Comments

2

I think you need to take SQL server Beginning lesson very serious.

This is what you want to do. a simple replace function will work on your question.

This query will filter the record for Id = 2 then replace function will replace Donovan with Paul.

Update TableName
set Names = replace(Names,'Donovan','Paul')
where Id = 2

1 Comment

Thanks for the reply but SQL Server isn't my focal point. This is why I came here to ask. And the where Id = 2 you anwsered doesn't fill my request. As Pondlife point me, this should be: UPDATE myTable SET Names= REPLACE(Names, 'Donovan', 'Paul') WHERE Names LIKE '%Donovan%' Thanks anyway
2
UPDATE table SET names = REPLACE(names,'Donovan','Paul')

Comments

2

Try this:

UPDATE
Tbl
SET Names = REPLACE(Names, 'Donovan','Paul')
WHERE PATINDEX('% Donovan %', Names) != 0

REPLACE (Transact-SQL)

PATINDEX (Transact-SQL)

or this, if you have full-text index on Names

UPDATE
Tbl
SET Names = REPLACE(Names, 'Donovan','Paul')
WHERE CONTAINS(Names, 'Donovan')

CONTAINS (Transact-SQL)

1 Comment

@Why Contains, He hasn't specified Full text search here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.