Select StoreID
,Schedule = stuff((Select concat('_'
,choose(SQLDayOfWeek,'Sun','Mon','Tue','Wed','Thu','Fri','Sat')+','
,IIF(Closed=1,'Closed',format(convert(datetime,TimeOpen),'HHmm')+',')
,IIF(Closed=1,'',format(convert(datetime,TimeClosed),'HHmm'))
)
From YourTable
Where StoreID=A.StoreID
Order By [SQLDayOfWeek]
For XML Path ('')),1,1,'') +';'
From (Select Distinct StoreID from YourTable) A
Group By StoreID
Order By StoreID
Возвращает