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?
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.
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.
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.
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
Homeworks?
I asume mysql:
update table t
set col1 = concat( col1, '.', col2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With