Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update column help

Status
Not open for further replies.

peac3

Technical User
Jan 17, 2009
226
AU
Hi guys,

I am trying to update the single column which contains many words
and want to update the word '%main%' to become '%optional%'

the table contains thousand of records.

Any idea how to do this?
database : MSSQL 2005

Thanks in advance guys,
 
Try
Code:
update myTable 
set ColWords = rtrim(ltrim(replace (' ' + ColWords + ' ',' main ',' optional '))) 
where ' ' + ColWords + ' ' LIKE '% main %'

Assuming that words are separated by spaces and you only want to update whole words.

PluralSight Learning Library
 
Hi makros,

Unfortunately, the columns words are not always the same,

Code:
ie
main product
main industrial
selected main code

would like become:
optional product
optional industrial
selected optional code

Thanks,
 
Did you try my solution? First to verify it, use SELECT instead of UPDATE, e.g.
Code:
select myWordColumn, replace(...) from myTable
where ' ' + myWordColumn + ' ' like '% main %'

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top