Miguel Flores Miguel Flores - 1 month ago 8
SQL Question

Two Rows in one column SQL Server

I have the following query:

SELECT tbl_SampledParts.Data
FROM tbl_SampledParts
INNER JOIN tbl_Inspection ON tbl_SampledParts.InspectionId = tbl_Inspection.InspectionId
WHERE (tbl_Inspection.InspectionDate BETWEEN '2016-10-26' AND '2016-11-03')


Result

That's what I get but I want to show the query results in only one column I tried using
COALESCE
and
XML
queries from answers in this page but I do not know how to add my inner join and where conditions. This query show the results like I want:

DECLARE @test NVARCHAR(max)

SELECT @test = COALESCE(@test + ',', '') + Data FROM tbl_SampledParts

SELECT @test


But like I said I need my inner join and where conditions mandatory, how can I do that?

UPDATE: Dates can change so that's the reason of why I need to use a where condition exactly like this
WHERE (tbl_Inspection.InspectionDate between '2016-10-26' and '2016-11-03')
.

Answer

You can combine both of your queries like this:

DECLARE @test NVARCHAR(max);

SELECT @test = COALESCE(@test + ',', '') + Data FROM (
SELECT tbl_SampledParts.Data FROM tbl_SampledParts 
INNER JOIN tbl_Inspection ON tbl_SampledParts.InspectionId = tbl_Inspection.InspectionId
WHERE (tbl_Inspection.InspectionDate between '2016-10-26' and '2016-11-03')
) as t;

SELECT @test;

that's it

Comments