MySql Search and replace using wildcards

Discussion in 'MySQL' started by carl_in_florida, Aug 29, 2007.

  1. #1
    I have a column that has a name. The names are currently listed

    "lastname, firstname" When it is a person. Sometimes it is listed "companyname, The" But other times the company is listed correctly.

    Is there a way to do a search an replace in Sql that says "where a comma is present reverse the stuff before the comma with the stuff after the comma"?

    I am presently doing it author by author by
    UPDATE data SET author = replace(author, "Luts, Frank", "Frank Luts");
    PHP:
    But this is getting old

    Thanks
     
    carl_in_florida, Aug 29, 2007 IP
  2. timothy247

    timothy247 Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am not that great at MySQL, I am better at standard SQL, but try the following command...

    The first part should select the Name (LastName, FirstName) and switch it to (FirstName LastName):

    SELECT CONCAT(SUBSTRING_INDEX(author, ',', -1), ' ', SUBSTRING_INDEX(author, ',', 1))
    FROM data

    Before I go any further though I want to make sure this works.
    Try it out and let me know.

    Then we can worry about updating the records.

    Tim :)
     
    timothy247, Aug 29, 2007 IP