How to concatenate records in two columns? [duplicate]

0

Let's say I have a table with two columns first and last name, for example:

Nome   | Sobrenome
-------+-----------
João   | Silva
Mike   | Corin
Carlos | Rodrigues

And I want to join the column of the name with the last name, to get the full name:

Nome
-----------------
João Silva
Mike Corin
Carlos Rodrigues

How can I do this? Without it being manually, perhaps with a function because you already have millions of records and doing this manually will take too long.

    
asked by anonymous 13.09.2017 / 18:27

1 answer

2

First of all, make backup of your database and related information. Sometimes very large changes can crash the crash browser .

The function to concatenate the tuples in the database is CONCAT() . It returns the sequence that results from the concatenation of the arguments, which may have one or more arguments.

In this case, to concatenate the values of the column nome and sobrenome , just execute the following code:

UPDATE tabela set nome = concat(nome, " ", sobrenome);

Next, I took the liberty of adding a blank space ( " " ) to add a space between the concatenation. If you do not want to just run this query instead of the first one:

UPDATE tabela set nome = concat(nome, sobrenome);

You can also view this SQL Fiddle that demonstrates how to accomplish what you want.

    
13.09.2017 / 18:29