Results 1 to 3 of 3
  1. #1

    Copy partial data from one field to another

    I have a database that has a column of prices, that I would like to parse and copy to another field. The problem, is there are also words in the same field. Ie. "$50 each"

    All of the data starts with "$", which I also don't want.

    So, my question is this:

    How can I copy just the dollar values from column a to column b within the same row, for all fields that start with the "$" ??

    Thanks in advance., a Digitally Justified Company
    Celebrating our 9th year in Business

    Proudly Hosting with CANADIAN bandwidth
    Managed Hosting, Multi-Domain Hosting, Colocation, Merchant Accounts

  2. #2
    Join Date
    Jan 2006
    Athens, Greece
    In your loop do a regex like this:
    PHP Code:
    $price 0;
    if (
    preg_match('/\$([0-9\.]+)\s/'$row['price'], $matches))
         if (isset(
    $matches[1])) $price $matches[1];

  3. #3
    Join Date
    Apr 2008
    Montreal, Qc., Canada
    Assuming the price doesn't contain a dot or a comma and that fieldA always start with a '$' and has a space and text following the price :

    PHP Code:
    mysql_query("UPDATE `yourTable` SET `fieldB` = SUBSTR(`fieldA`, 2, LOCATE('fieldA', ' ') + 2)"); 
    PHP Code:

    ['host'] = 'localhost';
    $db['user'] = 'root';
    $db['pass'] = 'yourPassword';
    $db['database'] = 'yourDatabase';
    $db['table'] = 'yourTable';

    $fieldA 'yourFieldA';
    $fieldB 'yourFieldB';

    if (
    $dbCon mysql_connect($db['host'], $db['user'], $db['pass']))
        if (
    mysql_select_db($db['database'], $dbCon))
    mysql_query("UPDATE `{$db['table']}` "
    "SET `{$fieldB}` = SUBSTR(`{$fieldA}`, 2, LOCATE('{$fieldA}', ' ') + 2)");
    'Updated ' mysql_affected_rows() . ' rows.';
    'Failed to connect to database.';
    'Wrong username/password combination.';
    Otherwise you'd have to loop each row using Steve's regex.
    Heymman - Beefy servers, tiny price !

Similar Threads

  1. How to replace data in MySQL field
    By golfer in forum Programming Discussion
    Replies: 7
    Last Post: 12-14-2011, 05:25 AM
  2. Replies: 2
    Last Post: 05-21-2010, 04:50 PM
  3. trying to post data from a dynamic field name?
    By lexington in forum Programming Discussion
    Replies: 3
    Last Post: 11-26-2007, 08:52 AM
  4. How to empty data from a MySQL field?
    By ITWeb LLC in forum Programming Discussion
    Replies: 5
    Last Post: 07-04-2005, 02:55 AM
  5. Best searching technique on large Mysql Text field data?
    By jmansoor in forum Programming Discussion
    Replies: 1
    Last Post: 03-28-2005, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts