How to concatenate the records of two columns?

Asked

Viewed 1,909 times

0

Let’s say I have a table with two columns name and surname, for example:

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

And I want to join the column of the name with the surname, to be the full name:

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

How can I do that? Without it being manually, maybe with a function because it already has millions of records and doing this task manually will take too long.

  • 2

    will create a new column in the table or simply select ?

1 answer

2


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


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

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

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

I’ve taken the liberty of adding a blank (" "), to add a space between concatenation. If you don’t want to just run this query instead of the first:

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

You can also see this SQL Fiddle that demonstrates the way to accomplish what you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.