I am concatenating two fields but it is showing me the end of one capo with the other. example: Pedro PabloCastro Ruíz
SELECT documento, concat(nombres, '', apellidos) as datos, nacimiento FROM jugador
I am concatenating two fields but it is showing me the end of one capo with the other. example: Pedro PabloCastro Ruíz
SELECT documento, concat(nombres, '', apellidos) as datos, nacimiento FROM jugador
The error is in the quotes should give a space
SELECT documento, concat(nombres, ' ', apellidos) as datos, nacimiento FROM jugador
The
CONCAT
what it does is concatenate two or more columns of a table, that is, visualize the data of 2 or more columns in one, as in your case has the columnNombre
andApellido
and theCONCAT
allows you to visualize both in a single field, you can add words in the fields to concatenate or blank spaces as is your case.
Keep in mind that CONCAT
has a serious problem with the values NULL
:
CONCAT()
returnsNULL
if any argument isNULL
.
CONCAT()
returnsNULL
if any argument isNULL
.
Therefore, in MySQL you must use CONCAT_WS
if values that you are going to concatenate can have values NULL
.
CONCAT_WS()
does not skip empty strings. However, it does skip any % co_of% values after the separator argument.
Also, in case you have to concatenate multiple columns and the separator is always the same, the code is cleaner.
For example:
If using NULL
you have to write this:
SELECT
CONCAT(col1, ' ', col2, ' ', col3, ' ', col4, ' ', col5, ... col999-y-999separadores)
as datos;
With CONCAT
you put the tab only once , at the beginning, and you're done:
SELECT
CONCAT_WS(' ', col1, col2, col3, col4, col5, ... col999)
as datos;
Let's see a demo of what happens if there are nulls between the values you want to concatenate.
With CONCAT_WS
you're never going to see Santiago because his last name is CONCAT
!!! :)
CREATE TABLE IF NOT EXISTS persona (
id SERIAL,
nombre VARCHAR(50),
apellido VARCHAR(50)
);
INSERT INTO persona (nombre, apellido) VALUES ('Pedro','Pérez');
INSERT INTO persona (nombre, apellido) VALUES ('Santiago',NULL);
INSERT INTO persona (nombre, apellido) VALUES ('Juan',' ');
-- Tratamiento de NULL con CONCAT_WS
SELECT CONCAT_WS(' ',nombre, apellido) datos FROM persona;
-- Tratamiento de NULL con CONCAT ... ¡terrible!
SELECT CONCAT(nombre, ' ',apellido) datos FROM persona;
NULL
datos
1 Pedro Pérez
2 Santiago
3 Juan
Resultado con CONCAT_WS:
datos
1 Pedro Pérez
2 NULL
3 Juan
PS: For more details you can consult this question:
You are not placing the space, simply a string without text, you should try this:
SELECT documento, concat(nombres, ' ', apellidos) as datos, nacimiento FROM jugador
^ Aquí
or even like this:
SELECT documento, datos = nombres + ' ' + apellidos, nacimiento FROM jugador
CONCAT
simply joins texts, one after the other, of those you pass as parameters separated by commas ( ,
), you can join in a single text all you want; for the case before joining the two VARCHAR
you want you must add, between both, the blank space that is what was missing from your original query.
The second case that I put you does practically the same, concatenate but "adding" the text to the right of +
to the text that is on the left being technically different from CONCAT
that creates a new text but finally gets the same result.
Although in the tags you indicate that it is for MySql, in case someone comes to this question interested in other languages, in other languages (remember now for example PostgreSQL or Multibase) the contatenación is done with ||
.
Your example would result in this way:
SELECT documento, nombres || ' ' || apellidos as datos, nacimiento FROM jugador