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
Release: 2024-11-12 17:44:02
Original
908 people have browsed it

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);
Copy after login

Example:

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

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

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%';
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template