Link to home
Start Free TrialLog in
Avatar of Paula DiTallo
Paula DiTalloFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of tigin44
tigin44
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paula DiTallo

ASKER

Brilliantly done!;-)