Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
SELECT
locationID,
availableDays
FROM lkup_locationByDay Ld
CROSS APPLY (
SELECT cast(ll.availableDays as VARCHAR) + ','
from lkup_locationByDay LL
where ld.ID = ll.id
FOR XML PATH('')
)AS cross1(AVAILlOCATIONS)
where locationID in (4,5,97,104,110,111,112,211,232,226,256)
order by locationID
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
GO
CREATE TABLE #tmp (locationID int, availableDays int)
INSERT INTO #tmp (locationID, availableDays)
VALUES (97,1), (97,2), (104,1), (104,2), (104,3), (104,4), (104,5)
SELECT locationID, LEFT(availableDays, LEN(availableDays) -1) as details
FROM (
SELECT DISTINCT t1.locationID,
stuff((
SELECT ' ' + CAST(availableDays as varchar(10)) + ', '
FROM #tmp t2
WHERE t1.locationID = t2.locationID
FOR XML PATH('')), 1, 1, '') as availableDays
FROM #tmp t1) a
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.