Fou
Fou

Reputation: 916

how to add extra word to each MySQL record

How to add extra word to each MySQL record.

Example Table:

|1    | David
|2    | Fin
|3    | Ronny
|4    | Lisa

Required:

|1    | T David
|2    | T Fin
|3    | T Ronny
|4    | T Lisa

Thanks for your help

Upvotes: 0

Views: 615

Answers (2)

Robert
Robert

Reputation: 8767

Utilize the CONCAT() string function to concatenate the extra word and the existing value:

UPDATE table SET name = CONCAT('T ', name);

Per the documentation:

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:

SELECT CONCAT(CAST(int_col AS CHAR), char_col);

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270795

Just concatenate the string literal with CONCAT() it into the column with an UPDATE statement:

UPDATE yourtable SET name = CONCAT('T ', name);

If you need to limit this to certain rows, be sure to use a WHERE clause:

UPDATE yourtable SET name = CONCAT('T ', name) WHERE <some condition>;

Upvotes: 1

Related Questions