How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?

0

2 Answers

To update a single column in a single table, you can use a combination of LOWER() and REPLACE():

UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_')) 

To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:

ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name; UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_')); 

Just be sure to update the data-type in the ALTER command to reflect your actual data-type.

5

When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D

My Answer though:

REPLACE(string1, find_in_string, replacementValue); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.