Doubt about CONCAT SQL

Asked

Viewed 43 times

0

I understand how it works about CONCAT of SQL-Server, but someone can help me?

For example: I have 3 tables: Team, Engine and Pilot.

used Concat with two tables Team and Motor

Select Concat(Equipe,' - ', Motor) EQUIPE 
  From Pilotos PI, Equipes E, Motores M
 Where P.CODIGO_EQUIPE = E.CODIGO_EQUIPE
   And P.CODIGO_MOTOR = M.CODIGO_MOTOR

See the result below worked, but how do I delete same name from the second column?

Resultado                       Quero resultado assim
EQUIPE                          EQUIPE
--------------------            -----------------------------
Cooper - Climax                 Cooper - Climax
Ferrari - Ferrari               Ferrari
Maserati - Maserati             Maserati

Look forward to.

1 answer

1

Can make a CASE ... WHEN to compare when the fields are equal does the CONCAT, otherwise returns only the field:

SELECT  CASE
          WHEN Equipe <> Motor THEN Concat(Equipe,' - ', Motor)
          ELSE Equipe
        END as EQUIPE
  From Pilotos PI, Equipes E, Motores M
 Where P.CODIGO_EQUIPE = E.CODIGO_EQUIPE
   And P.CODIGO_MOTOR = M.CODIGO_MOTOR

Note that WHEN Equipe <> Motor is directly copping the fields, if there are spaces, or difference in the box (uppercase/lowercase) will not return the expected.

  • It worked!!!! Thank you very much, Ricardo!!!

  • 1

    do not forget to accept the answer ;)

Browser other questions tagged

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