Search code examples
mysqlsqlrandom

How can replace a column of all records with random strings?


I have a table customers_info in my MySQL having a column 'address'.

I would like to replace the values of 'address' in all the rows with random texts (anything, for example, like xwdjduhyrmdz) for the privacy reason.

I found this SQL and tried it on phpmyadmin but didn't work for me.

UPDATE customer_info
SET address = LEFT(REPLACE(CAST(NEWID() AS CHAR(40)), '-', ''), @Characters)

How can I do this ?


Solution

  • This query will update all row of address column to random string of 6 character

    UPDATE `yourTable` SET `address` = CONCAT(
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1),
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1),
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1),
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1),
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1),
        SUBSTRING('abcdefghijklmnopqrstuvwxyz', FLOOR(RAND()*26) + 1, 1)
    );