Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat two table columns and update one with result

I have table that has two columns and I need to concatenate these two and update first column with result.
For example assume this is my table:

+----+-------+-------+
| id | col1  | col2  |
+----+-------+-------+
|  1 | text1 | text2 |
+----+-------+-------+
|  2 | text3 | text4 |
+----+-------+-------+

after concatenation my table should be:

+----+-------------+-------+
| id |    col1     | col2  |
+----+-------------+-------+
|  1 | text1.text2 | text2 |
+----+-------------+-------+
|  2 | text3.text4 | text4 |
+----+-------------+-------+

How can I do this using SQL?

like image 853
Ariyan Avatar asked Dec 05 '11 11:12

Ariyan


People also ask

How do I update two columns at once?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.

How do I concatenate two columns in a table?

SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function.

How do you concatenate two columns in query?

Select two or more columns that you need to merge. To select more than one column contiguously or discontiguously, press Shift+Click or CTRL+Click on each subsequent column. The order of selection sets the order of the merged values. Select Transform > Merge Columns.


2 Answers

Try this (for MySQL)

UPDATE your_table
SET col1 = CONCAT_WS('.', col1, col2)

and this for MS-SQL

UPDATE your_table
SET col1 =col1 || "." || col2
like image 198
Marco Avatar answered Oct 09 '22 22:10

Marco


Homeworks?

I asume mysql:

update table t
set col1 = concat( col1, '.', col2)
like image 41
dani herrera Avatar answered Oct 09 '22 21:10

dani herrera