If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > REPLACE Function for Bad Characters?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-04-02, 14:03
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Post REPLACE Function for Bad Characters?

I need to know if I can use the REPLACE function to remove bad characters from a data column. The data type is text and I would like to remove any comma's or quotes from the data. I assume that this would be the best way to remove these characters.

I've not been able to find much information on the REPLACE function so I'm not sure if the data can be replaced in the same column or if the
replacement data has to be placed in another column.

Any insight into this matter would be appreciated.

TechRick
Reply With Quote
  #2 (permalink)  
Old 09-04-02, 15:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
update yourtable
set textcol = replace(replace(textcol,','),'''')

if there is no replacement character, the search character is removed

i'm pretty sure you can nest oracle functions (i can't test it because i don't gots no oracle database)

to create a string consisting of a single quote, i think you have to code two consecutive ones, hence the four of them in a row like that

rudy
http://rudy.ca/
Reply With Quote
  #3 (permalink)  
Old 09-04-02, 15:56
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Thanks for the reply.

I think there is a difference however in the SQL that I'm using verses yours. I'm running on SQL 2000 and doing my queries through the Query Analyzer.

The format for the function command you suggested doesn't seem to work properly for me.

In my SAM's Learn SQL in 21 Days book I have this format given:

SELECT LASTNAME, REPLACE(LASTNAME, 'ST') REPLACEMENT
FROM CHARACTERS;

OUTPUT:
LASTNAME REPLACEMENT
--------------- ---------------
CHRISTINE CHRIINE
ADAMS ADAMS
COSTALES COALES

----------------------------------------------------------

The problem I have with this is that it takes the replacement data and puts it into another column. I want to keep the updated data in the same column. I guess I may need to live with a 'replacement' column and disregard the original?

Thanks again for your help.

TechRick
Reply With Quote
  #4 (permalink)  
Old 09-04-02, 16:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
dude, you posted in the "SQL and PL/SQL" forum, you should've posted in the "Microsoft SQL/Server" forum

i naturally assumed oracle, since REPLACE is an oracle function

in sql.server, the replacement string is not optional, so you have to specify it as a zero-length string

see http://msdn.microsoft.com/library/en...ra-rz_76lh.asp

so try

update yourtable
set textcol = replace(replace(textcol,',',''),'''','')

Reply With Quote
  #5 (permalink)  
Old 09-04-02, 16:30
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Smile

Sorry for the confusion. I didn't realize I was in the wrong place.

I'm a newbie to SQL and have only been using it for less than 2 weeks. I'm not yet familiar with all the differences in SQL formats.

I'll try to find my way to the correct forum in the future.

Thanks again for your help.

TechRick
Reply With Quote
  #6 (permalink)  
Old 09-04-02, 16:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
hey, no problem, i just thought i'd mention why you got an oracle answer instead of an sql/server answer the first time (i am not the moderator of either of these two forums)

so, did the sql/server version work?
Reply With Quote
  #7 (permalink)  
Old 09-04-02, 16:52
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Thumbs up

Rudy,

I just finished testing it and sure enough it did the trick! Thanks much for all the help.

One more question, why did you structure it this way:

replace(replace(textcol,',',''),'''','')


I thought it would need to be replace (texcol, ",", " ")

Why the extra replace and the added ),'''','')?

Just wondering.

Thanks again,
TechRick
Reply With Quote
  #8 (permalink)  
Old 09-04-02, 17:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
one removes commas, the other removes quotes

nesting them passes the result of one into the other

beats using two update stements, eh


rudy
Reply With Quote
  #9 (permalink)  
Old 09-04-02, 18:10
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Very nice.

I should have guessed that was the case.

Thanks again for all your help.

Best Regards,
TechRick
Reply With Quote
  #10 (permalink)  
Old 09-09-02, 20:11
sreekon sreekon is offline
Registered User
 
Join Date: Aug 2002
Posts: 3
I think we can also do this using TRANSLATE Function. I tested this one only in ORACLE and not in SQL-Server. I dont know whether we have one similar funtion in SQL-Server.

SELECT emp_id ,
emp_name,
TRANSLATE( emp_name, '%!@#$^', ' ' )
FROM employee_table ;

Regards,
Sreekon.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On