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

1

16

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.

Dutiable answered 30/8, 2017 at 14:23 Comment(5)
upc = CONCAT('k',upc)Electrochemistry
Maybe this would be a good time to look at normalizing your database.Flexible
I'm not sure what you are trying to do exactly as there is no WHERE condition for example, but adding a column / re-thinking your database design would seem a better idea than prefixing values.Oxendine
@Oxendine | This table contains products from a single vendor. We have many vendors that sell us the same products, with the same UPCs. This solution is so we can differentiate which vendor a specific product came from. Anyways, I'm just a dev :/ ...Dutiable
Possible duplicate of How to prepend a string to a column value in MySQL?Inventor
P
29

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)
Programmer answered 30/8, 2017 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.