Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate new string variable by concatenating two integer variables with MySQL

I apologize for the silliness of the question but am a complete neophyte with MySQL and am having trouble even reading the documentation for this. I have a table with two columns "homeid" and "indid", which are both integer data fields. I want to concatenate these together in a new variable "uid" with a hyphen. Desired output would look like this:

uid   homeid   indid
10-1  10       1
10-2  10       2
11-1  11       1

I have tried the following code, which does fine through generating the variable "uid" in which the concatenated variables should sit, but doesn't seem to work (though produces no error) beyond that:

ALTER TABLE table_name
add column uid varchar(10) FIRST; /*adds column uid to table "table_name" as first column*/

UPDATE table_name
SET uid=CONCAT('-' , homeid, indid);

Thanks in advance for your help (and patience).

like image 952
SMM Avatar asked Dec 31 '25 16:12

SMM


1 Answers

The function you need here is CONCAT_WS() to use a delimiting separator -, or change around the arguments to CONCAT():

UPDATE table_name SET uid = CONCAT_WS('-' , homeid, indid);

OR change around the arguments to CONCAT()

UPDATE table_name SET uid = CONCAT(homeid, '-', indid);
like image 168
Michael Berkowski Avatar answered Jan 02 '26 10:01

Michael Berkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!