Home » SQL & PL/SQL » SQL & PL/SQL » update using replace function
update using replace function [message #505969] Thu, 05 May 2011 15:48 Go to next message
jjj0923
Messages: 4
Registered: January 2009
Location: pennsylvania
Junior Member
I have a table called email that contains a field called email

I have a few records in the table that contain example@hotmial.com and want to change the hotmial to hotmail

I also have some that are line example@hotmail.co and I want to change those to hotmail.com

so - I want to do a substring search and replace.

can the replace do this for me in one select statement - how do I structure the sql to do this?

thanks in advance.
Re: update using replace function [message #505977 is a reply to message #505969] Thu, 05 May 2011 16:54 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/
Re: update using replace function [message #505979 is a reply to message #505977] Thu, 05 May 2011 17:02 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
For example:

update tab set col = replace(col,'hotmial.com','hotmail.com')
where col like '%hotmial.com';

replace

For the second case you would have to restrict the update to the rows that end with 'hotmail.co';
Perhaps:

update tab set col = replace(col,'hotmail.co','hotmail.com')
where col like '%hotmail.co';

[Updated on: Thu, 05 May 2011 17:06]

Report message to a moderator

Re: update using replace function [message #506002 is a reply to message #505979] Fri, 06 May 2011 00:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
can the replace do this for me in one select statement

A SELECT does not change the data, but yes you can do it in a single statement just use OR and DECODE to merge Thomas' statements.

Regards
Michel
Re: update using replace function [message #506003 is a reply to message #506002] Fri, 06 May 2011 01:04 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9100
Registered: November 2002
Location: California, USA
Senior Member
Still working on the same problem more than two years later. Amazing.

http://www.orafaq.com/forum/m/383197/43710/#msg_383197
Re: update using replace function [message #506007 is a reply to message #506003] Fri, 06 May 2011 01:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Note that in this old topic OP didn't understand the solution maybe after 2 years of cogitation of he will now understand it. Wink

Regards
Michel
Re: update using replace function [message #506010 is a reply to message #506007] Fri, 06 May 2011 01:26 Go to previous messageGo to next message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
The OP failed the same class twice (or more times?) so - the same homework is back again? /forum/fa/917/0/
Re: update using replace function [message #506067 is a reply to message #506010] Fri, 06 May 2011 07:01 Go to previous messageGo to next message
jjj0923
Messages: 4
Registered: January 2009
Location: pennsylvania
Junior Member
such a friendly group here... excuse me forgetting what I posted two years ago....you people have way too much time on your hands, are incredibly rude or just don't get way from your flat panels very often.

many thanks to the nice person who answered by question - ThomasG - you're kindness is exemplary in a world of mean spirited people.
Re: update using replace function [message #506071 is a reply to message #506067] Fri, 06 May 2011 07:06 Go to previous messageGo to next message
Littlefoot
Messages: 21823
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
It is not a problem that you forgot what you posted. Problem is that you didn't learn anything. This might be rude or cruel or whatever, but ... sorry, that's the way it is.
Re: update using replace function [message #506073 is a reply to message #506071] Fri, 06 May 2011 07:16 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Plus had you searched, you'd have certainly hit the answer!

So you ignore the rules, ask for help, learn nothing, ignore the rules again and wonder why you get short irritated replies?
Re: update using replace function [message #506160 is a reply to message #505969] Fri, 06 May 2011 14:17 Go to previous messageGo to next message
yassirrehman@gmail.com
Messages: 9
Registered: February 2008
Location: USA
Junior Member
Hello,

I would use oracle function regexp_replace to clean such data.
REPLACE replaces text based on literal, REGEXP_REPLACE on the other hand performs a replaces based on string pattern.
Remember REPLACE does not change the actual value in the table only output is changed. Following is an example.

SQL> SELECT ACCOUNT FROM TABLE1;

ACCOUNT
--------------------------------
CD'S NOT MINE
example@hotmial.com
abc@hotmale.com
zadsfdf@hotmial.com

1* select regexp_replace(account,'@:alpha:{7}','@hotmail') from table1
SQL> /

REGEXP_REPLACE(ACCOUNT,'@[[:AL
------------------------------------------------------------------------------
CD'S NOT MINE
example@hotmail.com
abc@hotmail.com
zadsfdf@hotmail.com


Any questions let me know.
Thanks,
Re: update using replace function [message #506163 is a reply to message #506160] Fri, 06 May 2011 14:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Does not fix example@hotmail.co.
Any questions let me know.

Regards
Michel
Re: update using replace function [message #506164 is a reply to message #506163] Fri, 06 May 2011 14:51 Go to previous messageGo to next message
yassirrehman@gmail.com
Messages: 9
Registered: February 2008
Location: USA
Junior Member
The idea behind using this function is to use pattern matching rather than literal. You can change the meta characters as per your specific need. Following is an other example that fixes your requirement.
Thanks & question appreciated.

SQL> select account from table1;

ACCOUNT
-----------------------------------
CD'S NOT MINE
example@hotmial.com
abc@hotmale.com
zadsfdf@hotmial.com
Thisoneisforyou@hotmail.co


select regexp_replace(account,'@:alpha:{7}.:alpha:{2,3}' , '@hotmail.com') Cadot_req from table1;
CADOT_REQ
----------------------------
CD'S NOT MINE
example@hotmail.com
abc@hotmail.com
zadsfdf@hotmail.com
Thisoneisforyou@hotmail.com
Re: update using replace function [message #506165 is a reply to message #506164] Fri, 06 May 2011 15:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
And if I want to change gmail to hotmail?

Regards
Michel
Re: update using replace function [message #506166 is a reply to message #506165] Fri, 06 May 2011 15:09 Go to previous messageGo to next message
yassirrehman@gmail.com
Messages: 9
Registered: February 2008
Location: USA
Junior Member
I dont see gmail anywhere. Be more clear/specific and post in detail.

Thanks.
Re: update using replace function [message #506167 is a reply to message #506166] Fri, 06 May 2011 15:23 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
@yassirrehman: The point is:

- The question was about correcting "@hotmial.com" and "@hotmail.co" specifically.
- Since the poster hasn't managed to figure out how to format a query in two years, it is possible that he will just copy/paste whatever he gets into a possibly existing live system.

So a query that changes EVERY ':alpha:{7}.:alpha:{2,3}' e-Mail to 'hotmail.com' might wreak havoc in that possibly existing live system.

That's why it's not a good idea to post a complete solution that doesn't fit the problem 100% when there is a high probability the original poster doesn't understand it.
Re: update using replace function [message #506169 is a reply to message #505969] Fri, 06 May 2011 16:11 Go to previous messageGo to next message
yassirrehman@gmail.com
Messages: 9
Registered: February 2008
Location: USA
Junior Member
Thomas,

What makes you think that this query will change every thing to hotmail.com and that this is a 100% solution for what was originally asked.
"One line Solution"

select regexp_replace(account,'@:alpha:{7}.:alpha:{2,3}' , '@hotmail.com') Cadot_req from table1;


Thanks & sensible/positive questions appreciated.
Re: update using replace function [message #506170 is a reply to message #506169] Fri, 06 May 2011 16:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You seems to not understand that if OP has some errors he mentioned in his table about hotmail addresses, he did say that he has ONLY hotmail addresses and your query will change ALL @xxxxxxx.xxx to @hotmail.com which is most likely NOT what OP wanted.
And so you didn't answer the question which can read as "I want to ONLY change @hotmial.com and @hotmail.co to @hotmail.com". He didn't asked to change something else to @hotmail.com.

In addition, Thomas pointed you to the fact that OP does not understand SQL and will just copy and paste your statement without understanding it and injure his (or rather his client) data.
Do you understand what we meant?

Regards
Michel
Re: update using replace function [message #506171 is a reply to message #506170] Fri, 06 May 2011 16:40 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
yassirrehman,

Quote:

What makes you think that this query will change every thing?


I'm terribly sorry. I just assumed that you actually posted the actual output of the query you actually ran, where it looked like the query actually worked. As it turns out, it doesn't work. So the original posters data is safe.

SQL>
SQL> CREATE TABLE table1 (account VARCHAR2(100));
Table created.
SQL>
SQL> INSERT INTO table1 VALUES ('example@hotmial.com');
1 row created.
SQL> INSERT INTO table1 VALUES ('abc@hotmale.com');
1 row created.
SQL> INSERT INTO table1 VALUES ('zadsfdf@hotmial.com');
1 row created.
SQL> INSERT INTO table1 VALUES ('Thisoneisforyou@hotmail.co');
1 row created.
SQL>
SQL>
SQL> select regexp_replace(account,'@:alpha:{7}.:alpha:{2,3}' , '@hotmail.com')
  2   Cadot_req from table1;
CADOT_REQ
--------------------------------
example@hotmial.com
abc@hotmale.com
zadsfdf@hotmial.com
Thisoneisforyou@hotmail.co
SQL>


I apologize for my mistake, next time I will check if you faked the output first.

[Updated on: Fri, 06 May 2011 16:41]

Report message to a moderator

Re: update using replace function [message #506211 is a reply to message #506171] Sat, 07 May 2011 01:30 Go to previous message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
It is not safe if OP has a valid address like "@netmail.com" and don't wnat to see it replace by "@hotmail.com".

Regards
Michel
Previous Topic: SQL - Purge Data (2 Merged)
Next Topic: Best fit sum
Goto Forum:
  


Current Time: Sun Nov 24 15:33:13 CST 2024