Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stored Procedure Replace function

Posted on 2009-04-15
7
Medium Priority
?
228 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

886 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