Solved

Stored Procedure Replace function

Posted on 2009-04-15
7
215 Views
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?
0
Comment
Question by:LATurk
  • 5
  • 2
7 Comments
 
LVL 31

Expert Comment

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

Expert Comment

by:RiteshShah
ID: 24147403
you can do like this

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

Expert Comment

by:RiteshShah
ID: 24147423
refined version for you.



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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LATurk
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.
0
 
LVL 31

Accepted Solution

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

Expert Comment

by:RiteshShah
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
0
 

Author Closing Comment

by:LATurk
ID: 31570401
Thanks!  Works perfectly!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 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