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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and replace values in the table in SQL Server??

Status
Not open for further replies.

qajussi

Programmer
Mar 22, 2004
236
US
How do you replace values of one column in several tables?
What script do you use?

I have a table containing office symbols.

listOfficeSymbol
ID officeName
1 English <==== need to change it to Tiger
2 Math <==== Rabbit
3 Music <==== Eagle
Etc...

And there are other tables containing value of officeName.

tblContact
ID Name officeName
c1 Smith English
c2 Doe Math
Etc...

How do i go about changing the value of office Name
Can you give me a step by step instruction??
THanks much in advance..
 
1 English <==== need to change it to Tiger
2 Math <==== Rabbit
3 Music <==== Eagle


update table set officename = replace(officename ,'English','Tiger')

update table set officename = replace(officename ,Math,'Rabbit')
update table set officename = replace(officename ,'Music','eagle')

“I sense many useless updates in you... Useless updates lead to defragmentation... Defragmentation leads to downtime...Downtime leads to suffering..Defragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
Update listOfficesymbols
Set OfficeName = Replace(OfficeName, 'English', 'Tiger')


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Personally I'd do something like:
Code:
Update listOfficesymbols
set OfficeName =
	case OfficeName
		when 'English' then 'Tiger'
		when 'Math' then 'Rabbit'
		when 'Music' then 'Eagle'
	end
--where OfficeName in ('English', 'Math', 'Music')
How to replace one column in several tables... repeat this process for each table individually.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top