Stored Procedure Replace function

Posted on 2009-04-15
Last Modified: 2013-11-05
If I have a simple select statement like this:
Select Name from tblNames where ID = 1
I want to replace any occurances in Name with the following:
Replace & with &
Replace " with "
Replace - with –
Is there an easy way to do this in a stored procedure?
Question by:LATurk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 31

Expert Comment

ID: 24147391
select replace(name,'&','&')
LVL 31

Expert Comment

ID: 24147403
you can do like this

create proc replaceReturn
Select replace(Name,'&','&') as name from emps
LVL 31

Expert Comment

ID: 24147423
refined version for you.

create proc replaceReturn
Select replace(replace(replace(Name,'&','&'),'"','"'),'-','&ndash') as name from tblNames where id=1

Open in new window

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 24147453
But what if one name had all three in it.  
For Example: John "the man" & Smith-Jones
I know that's ridiculous but I want to be sure none of the characters are in this field.
LVL 31

Accepted Solution

RiteshShah earned 500 total points
ID: 24147482
it will work for sure, see my last post. i did it practically and it worked
LVL 31

Expert Comment

ID: 24147492
see this is what you want, right?

Select replace(replace(replace('John "the man" & Smith-Jones','&','&'),'"','"'),'-','&ndash')

will be looked like

John "the man" & Smith&ndashJones

Author Closing Comment

ID: 31570401
Thanks!  Works perfectly!

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question