Running a SQL query to replace certain characters in a table

Discussion in 'MySQL' started by MidoriWeb, Jan 11, 2007.

  1. #1
    We have a database that contains many characters and symbols we no longer want (or that we need to replace with something else). For example... We have some records in a database that contain the copyright symbol:

    ©

    And we want to replace that with:

    ©

    Our table is titled Product_Descriptions and the column we want to run this query on is ProductDescription.

    I've already searched and I found this:

    UPDATE <table>
    SET name = REPLACE(LTRIM(RTRIM(name)), '©', '&copy;')

    That should work right? But what do I enter to run this on my specific table/column? I know I have to replace the <table> from above with my own input... but can someone guide me in the right direction. I'm a noob when it's comes to SQL queries.
     
    MidoriWeb, Jan 11, 2007 IP
  2. stock_post

    stock_post Prominent Member

    Messages:
    5,213
    Likes Received:
    249
    Best Answers:
    0
    Trophy Points:
    310
    #2
    Go to your cpanel
    and then to mysql admin

    select the table

    And select query part (not sure what they call it)

    And type you query and click Go

    Thanks
     
    stock_post, Jan 11, 2007 IP
  3. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Sorry... I should have clarified... this on on MS SQL.
     
    MidoriWeb, Jan 11, 2007 IP
  4. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    An update...

    I found the following command to run against my database:

    UPDATE Products_Descriptions
    SET ProductDescription = REPLACE(LTRIM(RTRIM(ProductDescription)), '©', '&copy;')

    However... when I try to run it it throws the following error:

    Server: Msg 8116, Level 16, State 2, Line 1
    Argument data type text is invalid for argument 1 of rtrim function.

    Any ideas?
     
    MidoriWeb, Jan 11, 2007 IP
  5. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    I tried:

    UPDATE Products_Descriptions
    SET ProductDescription = REPLACE(ProductDescription,'©','&copy;')

    But got this error:

    Server: Msg 8116, Level 16, State 1, Line 1
    Argument data type text is invalid for argument 1 of replace function.
     
    MidoriWeb, Jan 11, 2007 IP
  6. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    I've done some more research and it appears since the database column is of type TEXT I can't run the replace function.

    So... next best thing is to write a custom ASP Script. This isn't the right forum for this so I created a new thread here:

    http://forums.digitalpoint.com/showthread.php?p=2098579

    If anyone can help me I'd really appreciate it :)
     
    MidoriWeb, Jan 11, 2007 IP
  7. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I don't know ASP but in PHP it'd just be something like:
    $replace = array(
    '©'  =>  '&copy;',
    );
    
    $conn = mssql_connect('SQLSERVER', 'user', 'password') or die('Could not connect');
    mssql_select_db('databasename', $conn) or die('No such database');
    
    $res = mssql_query('SELECT ProductID,ProductDescription FROM Product_Descriptions',$conn) or die ('Query failed');
    
    while ( $row = mssql_fetch_assoc($res) ) {
        foreach ( $replace as $old => $new ) {
          if ( strpos($row['ProductDescription'],$old) ) {
            $newDesc = str_replace($old,$new,$row['ProductDescription']);
            mssql_query("UPDATE Product_Descriptions SET ProductDescription = '$newDesc' WHERE ProductID=".$row['ProductID'],$conn);
          }
        }
    }
    
    mssql_close($conn);
    
    PHP:
    If there's a lot of rows, you might want to add some limits and do X at a time.
     
    rodney88, Jan 12, 2007 IP
  8. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I gave you the ASP/MS SQL solution in your other thread, try that.
     
    smallbuzz, Jan 12, 2007 IP