Home >Database >Mysql Tutorial >How do I Add a String Prefix to Column Values in MySQL?

How do I Add a String Prefix to Column Values in MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-12 17:44:02920browse

How do I Add a String Prefix to Column Values in MySQL?

Adding a String Prefix to Column Values in MySQL

To prepend a string to a column value in MySQL, you can employ the CONCAT function. This function combines multiple strings into a single one.

Syntax:

UPDATE table_name
SET column_name = CONCAT('string_to_prepend', column_name);

Example:

To add the string "test" to the beginning of the "name" column in the "employees" table:

UPDATE employees
SET name = CONCAT('test', name);

Example with Conditional Update:

If you only want to update rows where the current value of the column does not already have the string "test" prepended, you can use the following query:

UPDATE employees
SET name = CONCAT('test', name)
WHERE name NOT LIKE 'test%';

This ensures that "test" is only added to column values that don't contain it.

The above is the detailed content of How do I Add a String Prefix to Column Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn