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

Replace function: what instead?

Status
Not open for further replies.

davideart

Programmer
Apr 4, 2001
55
0
0
IT
Hy, I need your help about this problem: I have a table with 20.000 records (more or less) and I need to substitute a certain character in this table's fields with another (e.g. substitute "@" with "a").I've found the REPLACE function but it seems to work only with SQL Server SP2. Do you know of any other Stored Procedure that can do the work? It's very important for me. Thank you for your help.
 
What is your SQL version?
REPLACE works in my version MS-SQL 7 SP3

br
Gerard
 
Hy, foxbox, you are right, I forgot to specify that that the SQL version on which REPLACE doesn't run is the 6.5. Hope to hear good news.Bye
 
I was just going to post this exact same problem. I'll wait with you for the good news.
 
I figured out a way to do it using STUFF and CHARINDEX
stuff(column name, charindex('@', column name),1, 'a') THis will replace your @'s with a's but if you have a column without an @ it gives you a null. I think you may have to use a case to solve that problem, I'm working on that now, but I hope this helps you.
 
I worked around the problem using the followin' script, which uses Charindex and update. I don't know if it's the best solution but it works.
UPDATE tablename
SET fieldname = substring(fieldname,1,CHARINDEX('@',fieldname)-1)+substring(fieldname,CHARINDEX('@',fieldname)+1,DATALENGTH(fieldname)-CHARINDEX('@',fieldname))
WHERE CHARINDEX('@',fieldname) > 0
WHILE (@@rowcount > 0)
BEGIN
UPDATE tablename
SET fieldname = substring(fieldname,1,CHARINDEX('@',fieldname)-1)+substring(fieldname,CHARINDEX('@',fieldname)+1,DATALENGTH(fieldname)-CHARINDEX('@',fieldname))
WHERE CHARINDEX('@',fieldname) > 0
END
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top