holemania
asked on
SQL Query - concatenate multiple fields into 1
Hello experts,
Not sure how to go about doing this. I have about 5 left outer join to various databases with similar fields. Now I need to join these 5 fields into one. I tried doing the following, but if 1 is blank, then none show up.
(DB1.ID + '' + DB2.ID + '' + DB3.ID + '' + DB4.ID + '' DB5.ID) AS COMBINE_IDS
Here's my query:
Not sure how to go about doing this. I have about 5 left outer join to various databases with similar fields. Now I need to join these 5 fields into one. I tried doing the following, but if 1 is blank, then none show up.
(DB1.ID + '' + DB2.ID + '' + DB3.ID + '' + DB4.ID + '' DB5.ID) AS COMBINE_IDS
Here's my query:
SELECT IM.ITEM_ID, IM.ITEM_DESCRIPTION, IM.MFG_ID
(MFG1.MFG_ID + '/' + MFG2.MFG_ID + '/' + MFG3.MFG_ID + '/' + MFG4.MFG_ID + '/' + MFG5.MFG_ID) AS DB_MFG
FROM ITEM_MASTER IM
LEFT OUTER JOIN
(SELECT I1.ITEM_ID, I1.MFG_ID
FROM [SRV1].DB1.DBO.ITEM_MASTER I1
) MFG1 ON MFG1.ITEM_ID = IM.ITEM_ID
LEFT OUTER JOIN
(SELECT I2.ITEM_ID, I2.MFG_ID
FROM [SRV2].DB2.DBO.ITEM_MASTER I2
) MFG2 ON MFG2.ITEM_ID = IM.ITEM_ID
LEFT OUTER JOIN
(SELECT I3.ITEM_ID, I3.MFG_ID
FROM [SRV3].DB3.DBO.ITEM_MASTER I3
) MFG3 ON MFG3.ITEM_ID = IM.ITEM_ID
LEFT OUTER JOIN
(SELECT I4.ITEM_ID, I4.MFG_ID
FROM [SRV4].DB4.DBO.ITEM_MASTER I4
) MFG4 ON MFG4.ITEM_ID = IM.ITEM_ID
LEFT OUTER JOIN
(SELECT I5.ITEM_ID, I5.MFG_ID
FROM [SRV5].DB5.DBO.ITEM_MASTER I5
) MFG5 ON MFG5.ITEM_ID = IM.ITEM_ID
WHERE IM.MFG_ID IS NOT NULL
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER