2

I'm trying to get a row with the concatenation of a welders names. This is what i've got:

SELECT  jsd1.JuntaSoldaduraID, 
        REPLACE(RTRIM((SELECT s1.Nombre + ' ' + s1.ApPaterno + ' ' + 
            s1.ApMaterno + '' + CAST('' AS VARCHAR(MAX)) + ' ' 
FROM JuntaSoldaduraDetalle jsd
INNER JOIN  Soldador s1 on s1.SoldadorID = jsd.SoldadorID
WHERE (jsd1.JuntaSoldaduraID = jsd.JuntaSoldaduraID) 
          and (jsd.TecnicaSoldadorID = 2)
FOR XML PATH (''))),' ',', ') AS NombreSoldador
FROM JuntaSoldaduraDetalle jsd1
INNER JOIN Soldador s
ON s.SoldadorID = jsd1.SoldadorID
GROUP BY jsd1.JuntaSoldaduraID

I'm able to get the information that i want but with a little problem. What I want is "John Smith, David Rogers, Peter Simons" etc.. in other words, full names separated by commas. But i'm receiving "John, smith, David, Rogers, Peter, Simons"..

Any help appreciated.

Thanks in advance.

2 Answers 2

3

Your REPLACE will replace all spaces with a comma. Instead, make the comma part of your query and use STUFF to remove the first occurrence of the comma. You might also want to incorporate the use of COALESCE in case any of the name fields are NULL.

SELECT  jsd1.JuntaSoldaduraID, 
        STUFF((SELECT ', ' + COALESCE(s1.Nombre + ' ','') 
                           + COALESCE(s1.ApPaterno + ' ','') 
                           + COALESCE(s1.ApMaterno,'')
                   FROM JuntaSoldaduraDetalle jsd
                       INNER JOIN  Soldador s1 
                           on s1.SoldadorID = jsd.SoldadorID
                   WHERE jsd1.JuntaSoldaduraID = jsd.JuntaSoldaduraID 
                       and jsd.TecnicaSoldadorID = 2
                   FOR XML PATH ('')),1,2,'') AS NombreSoldador
    FROM JuntaSoldaduraDetalle jsd1
        INNER JOIN Soldador s
            ON s.SoldadorID = jsd1.SoldadorID
    GROUP BY jsd1.JuntaSoldaduraID
Sign up to request clarification or add additional context in comments.

3 Comments

hey Joe, i've tried this out and it works great. Thank you so much!
+1 my approach of teach a man to fish and such clearly didn't pay off.
@Lieven: Sometimes, a man may come from the desert and you have to spend a little more time explaining what a rod and reel are. :-)
0

You are replacing single spaces in the result with a comma and a space.

What is the result if you remove the REPLACE from the query?

2 Comments

I could remove the REPLACE, but still i'll have the names without separation, just: "john smith david jones peter simons", etc.
You could append the comma in the inner select and only remove the last comma in the outer select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.