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!

Is there a better way to Update fields? 1

Status
Not open for further replies.

eyhandle

Programmer
Feb 24, 2003
17
US
Is there a better way to Update fields?Below works, but is there a better way?


1.Delete all hyphens, apostrophes, blankd, periods, II,

Update tablename
set first_name= replace(first_name, 'II', "")

Update tablename
set first_name= replace(first_name, '-', "")


Update tablename
set first_name= replace(first_name, ''', "")


Thanks so much
 
You can nest the executions of the REPLACE function. Then you only need to make one pass of the table. I recommend adding a Where clause so the query only updates rows that contain the characters to be changed.

Update tablename Set first_name =
replace(replace(replace(first_name,' II',''),'-',''),char(39),'')
Where charindex(' II',first_name)>0
Or charindex('-',first_name)>0
Or charindex(char(39),first_name)>0

Note: char(39) is a single quote '

You need to be carefule about changing II to empty space. A name might contain that string. I added a space before the 'II' in the code so the query checks for ' II'. If you want to get the best answer for your question read faq183-874 and faq183-3179.
Terry L. Broadbent - DBA
SQL Server Page:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top