Jump to content

[SOLVED] Removing part of a record?


woolyg

Recommended Posts

Hi all,

 

I have a field that contains the email addresses of all subscribers to a topic. Like this

 

|-----------|------|

| Topic ID  | Email |

|-----------|------|

|    23      | [email protected], [email protected], [email protected]|

|-----------|------|

|    24      | [email protected], [email protected], [email protected]|

 

 

What syntax would I use to remove only [email protected] from the Email field of Topic ID 24, and keep the other info in the field?

 

All help appreciated,

Woolyg.

 

Link to comment
https://forums.phpfreaks.com/topic/70317-solved-removing-part-of-a-record/
Share on other sites

Hi Woolyg,

this query removes email from any position in Email field:

 

UPDATE `Your Table` SET `Email` = TRIM(BOTH ", " FROM REPLACE(REPLACE(`Email`,
  "[email protected]",
  ""), ", , ", ", "))
WHERE `Topic ID` = 24;

 

If you need a little explanation, first function (TRIM) removes unnecessary separator when an email is removed from the beginning or from the end of a list, second function (REPLACE) removes unnecessary separator when an email is removed from the middle of a list, third function (REPLACE) removes given email address.

 

Michal

Michal,

 

I'm implementing exactly the code you set out, but what I'm finding when an email is removed from the middle of a list, it's removing the comma straight after it, which is fine, but it's also removing the comma at the very end of the list, which is kinda fooling with my population coding. Is there a way to remove only the mail & the comma, without touching anything else?

 

eg

 

[email protected],

 

 

- Cheers,

Woolyg

Woolyg,

 

removing query has been written to conform to your example data and its separator ", ". If you changed your format and Email value looks like:

|[email protected], [email protected], [email protected],|

 

removing query will look like:

UPDATE `Your Table` SET `Email` = TRIM(LEADING " " FROM TRIM(LEADING "," FROM TRIM(TRAILING " ," FROM REPLACE(REPLACE(`Email`,
  "[email protected]",
  ""), ", , ", ", "))))
WHERE `Topic ID` = 24;

 

But remember this new format is not very consistent, you have ", " separator between emails and "," at the end.

 

If you are interested, I would chose one of these formats:

Easier to read from

|[email protected],[email protected],[email protected]|

or

Easier to write to

|[email protected],[email protected],[email protected],|

 

Try to play with TRIM, BOTH, LEADING, TRAILING, REPLACE.

 

Michal

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.