Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Strip Text Strings From A Field?

This is a discussion on Strip Text Strings From A Field?, within the MySQL section. This forum and the thread "Strip Text Strings From A Field?" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Strip Text Strings From A Field?
Jason
post May 23 2008, 09:27 AM
Post #1


Master of the Universe
Group Icon

Posts: 1,297
Joined: 15-February 08
From: London, England


OK, a guy has a website with a price field. Unforunately this is set as varchar and many numbers are in the following format.

1000 EUR

I need to remove the EUR part.

Can this be done with MySQL? Or do I need to use PHP to analyse the string?


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post May 23 2008, 09:42 AM
Post #2


Squeeze Machine
Group Icon

Posts: 760
Joined: 13-February 08
From: Catching the squeezed drips downunder.


if it's always in the format 1000 eur or ( nnnn ccc) where n= number and c= currency, you can use

CODE
SELECT SUBSTR(price, 1, (LENGTH(price)-4)) as price_integer FROM here WHERE this=that


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
Jason
post May 23 2008, 09:51 AM
Post #3


Master of the Universe
Group Icon

Posts: 1,297
Joined: 15-February 08
From: London, England


QUOTE (Rakuli @ May 23 2008, 03:42 PM) *
if it's always in the format 1000 eur or ( nnnn ccc) where n= number and c= currency, you can use

CODE
SELECT SUBSTR(price, 1, (LENGTH(price)-4)) as price_integer FROM here WHERE this=that


The price varies. Some prices do not have EUR.


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post May 23 2008, 10:12 AM
Post #4


Squeeze Machine
Group Icon

Posts: 760
Joined: 13-February 08
From: Catching the squeezed drips downunder.


Unfortunately there isn't a way to select part of a column using regular expressions. You can select a whole column based on a regex and check a regex in the select column list but not actually pull part of that regex out. SQL is quite a simple language so doesn't really have a function to achieve that sort of thing.

If I were dealing with this database, I would write a php script that pulls all of the prices out and their tables primary key, runs a preg_replace over all the prices to filter them down to their numeric values, alters the table price column to be integer or double and then updates the records with the primary key and updated value.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
rewake
post May 23 2008, 10:15 AM
Post #5


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


You can use MySQL's REPLACE() string function...

SELECT TRIM(REPLACE(`price`,'EUR','')) as `newPrice` FROM `table`

The TRIM() function will get rid of any spaces.

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
Jason
post May 24 2008, 08:34 AM
Post #6


Master of the Universe
Group Icon

Posts: 1,297
Joined: 15-February 08
From: London, England


QUOTE (rewake @ May 23 2008, 04:15 PM) *
You can use MySQL's REPLACE() string function...

SELECT TRIM(REPLACE(`price`,'EUR','')) as `newPrice` FROM `table`

The TRIM() function will get rid of any spaces.

Rich


If this works... I will be very happy indeed. I need to do this over the weekend and meh, I want to relax and watch movies. Not work.


--------------------
Go to the top of the page
 
+Quote Post
velo
post May 25 2008, 05:45 AM
Post #7


Rapid Squeezer
****

Posts: 182
Joined: 19-February 08
From: Netherlands


And if that doesn't work, maybe you export the data to a csv and edit it there. Then import it again. Or maybe create some nice update strings using the concat function of excel. Anyway, i'd say the SQL thing should do the trick.

Good luck


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

fresh-style.nl - small webdesign & development projects
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 jackfranklin 199 25th March 2008 - 05:26 PM
Last post by: Linda
No New Posts   11 jamesicus 454 12th April 2008 - 01:48 PM
Last post by: jamesicus
No New Posts   3 Jason 224 2nd April 2008 - 01:41 PM
Last post by: karinne
No New Posts   3 Jason 297 14th April 2008 - 03:49 PM
Last post by: Rakuli
No New Posts   1 paintingtheweb 213 17th May 2008 - 04:48 AM
Last post by: MikeHopley