Flattening Data: SQL Server 2008

Techies--
I am trying to flatten a table -- I am almost there, but I can't get rid of the separator I dubbed in when I don't need it. In a nutshell, here is what this is about. I have a Site table and I have a Facility table. I can have multiple facilities for a Site, so I want to just add the facilities found like so: 200;400

So instead of:
SiteID      FacilityNumber
200          200
200          400

I want to create:
SiteID      FacilityNumber
200         200;400

What I am getting back from my code however is this:
SiteID      FacilityNumber
200         200;400;

Take a look at what I do have--and please advise on how I can get rid of that last ';'.
SELECT s1.SiteID,
     (SELECT (replace(f.Number,'','') + ';')
       FROM Facility f, [Site] s2
        WHERE s2.SiteID = f.SiteID
          and s2.SiteID = s1.SiteID
         ORDER by f.Number
          FOR XML PATH('')) AS [Number]
     FROM [Site] s1
   GROUP BY SiteID

Open in new window

Paula DiTalloIntegration developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tigin44Commented:
try this

SELECT s1.SiteID,
     STUFF((SELECT ';'+f.Number
       FROM Facility f, [Site] s2
        WHERE s2.SiteID = f.SiteID
          and s2.SiteID = s1.SiteID
         ORDER by f.Number
          FOR XML PATH('')),1,1,'') AS [Number]
     FROM [Site] s1
   GROUP BY SiteID
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paula DiTalloIntegration developerAuthor Commented:
Brilliantly done!;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.