Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prepend string to a field value in MySQL?

Tags:

sql

mysql

prepend

I have a huge table of products from a specific vendor with a UPC column. I need to differentiate these products' UPCs from other vendors. The current idea is to prepend all of these UPCs with the letter a.

UPDATE abc_items SET upc = 'a' + upc

is basically how I imagine doing something like this, but I know it will not work.

like image 566
ma77c Avatar asked Aug 30 '17 14:08

ma77c


People also ask

How do I add text to a selected query in MySQL?

To add/ concatenate text values within a select clause, you can use concat() function.


1 Answers

Just use the CONCAT function.

CONCAT(str1,str2,...)

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 nonbinary string form.

UPDATE abc_items SET upc = CONCAT('k', upc)
like image 121
Neodan Avatar answered Oct 11 '22 15:10

Neodan