Q:

database update query?

suppose a table is like this
ID            Desc
1          I am a boy
2          I am a boy xyz
3          I am a boy abc
4          xyzab I am a boy
5          life is beautiful
6          blank

now my requirement is whenever in a row the phrase “I am a boy” is found it will be removed but the rest of the part will remain as it is. If that phrase is not there there will be no change. So for the above table after the query it will look like this
ID          Desc
1          <blank>
2           xyz
3           abc
4          xyzab
5         life is beautiful
6          blank

A:

You can try the following replacement.  Note that I wrap the replaced description column to remove any whitespace leftover from removing the phrase I am a boy.
UPDATE yourTable
SET Desc = TRIM(REPLACE(Desc, 'I am a boy', ''))
WHERE Desc LIKE '%I am a boy%'

The WHERE clause restricts the UPDATE operation to those records containing the boy string only.  In this particular case, it might be redundant.  But in general adding a WHERE clause can save time by avoiding modifying a record when no change is intended.  Also, in general (though not here), a WHERE clause can even speed up the update when the where column has an index.

