Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extracting and parsing XML from fields (CRM related)


Extracting and parsing XML from fields (CRM related)

Author
Message
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45434 Visits: 39942
LutzM (1/7/2012)
You'll need the DelimitedSplit8K function to split the data. If you don't have it in your tool box already, search this site for it (originally posted by Jeff Moden).


Lutz,

Thank you for the reference but you might want to update the "Delimited Split Function" link in your signature to the newer, more performant version at the following link...
http://www.sqlservercentral.com/articles/Tally+Table/72993/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7021 Visits: 13559
Jeff Moden (1/8/2012)
LutzM (1/7/2012)
You'll need the DelimitedSplit8K function to split the data. If you don't have it in your tool box already, search this site for it (originally posted by Jeff Moden).


Lutz,

Thank you for the reference but you might want to update the "Delimited Split Function" link in your signature to the newer, more performant version at the following link...
http://www.sqlservercentral.com/articles/Tally+Table/72993/


Done as requested :-D
Thank you for the updated link and the reminder to change my signature!



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45434 Visits: 39942
My pleasure. It's an honor that you're carrying some of my articles as a link in your signature. Thanks, Lutz.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
phual
phual
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 163
I've taken the suggested code and played around with it to get a better understaning of how it works. The fruits of my playing are below:


WITH TempTable AS (
SELECT ConstraintBasedGroupId, Constraints
FROM ConstraintBasedGroupBase
)
SELECT
TempTable.ConstraintBasedGroupId,
SUBSTRING(item,CHARINDEX('{',item)+1,CHARINDEX('}',item)-CHARINDEX('{',item)-1) AS TeamId
FROM TempTable
CROSS APPLY
DelimitedSplit8K(REPLACE(Constraints,'||','|'),'|') split
) AS ResourceGroupToTeam



This is fantastic, in that it does what I want it to (it newly introduces me to CROSS APPLY and removes the complexity of messing around with the XML). However, I've fallen foul of a different (and I suspect very simple) problem at the next hurdle.

The above code returns the IDs in two fields. My first though was to use the above as a derrive table with the other two tables involed to get the text, something like this:


SELECT ResourceGroupBase.Name AS ResourceGroupName,
TeamBase.Name AS TeamName
FROM ResourceGroupBase
INNER JOIN
(
WITH TempTable AS (
SELECT ConstraintBasedGroupId, Constraints
FROM ConstraintBasedGroupBase
)
SELECT
TempTable.ConstraintBasedGroupId,
SUBSTRING(item,CHARINDEX('{',item)+1,CHARINDEX('}',item)-CHARINDEX('{',item)-1) AS TeamId
FROM TempTable
CROSS APPLY
DelimitedSplit8K(REPLACE(Constraints,'||','|'),'|') split
) AS ResourceGroupToTeam
ON ResourceGroupBase.ResourceGroupId = ResourceGroupToTeam.ConstraintBasedGroupId
INNER JOIN
TeamBase
ON ResourceGroupToTeam.TeamId = TeamBase.TeamId



I've never worked with 'WITH' before, so I'm sure it is has restrictions on its use that I'm falling foul of. Given that this code block will form the basis of other queries (and so it would be nice if it could be discretely inserted as a unit), how should I write the code above to make the final query (ResourceGroupName and TeamName) work?

Thanks for the responses so far.

Stuart
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2095 Visits: 22813
Common table expressions (the 'WITH' bit) go at the start of the query.

WITH TempTable AS (
SELECT ConstraintBasedGroupId, Constraints
FROM ConstraintBasedGroupBase
),
ResourceGroupToTeam AS (
SELECT
TempTable.ConstraintBasedGroupId,
SUBSTRING(item,CHARINDEX('{',item)+1,CHARINDEX('}',item)-CHARINDEX('{',item)-1) AS TeamId
FROM TempTable
CROSS APPLY
DelimitedSplit8K(REPLACE(Constraints,'||','|'),'|') split
)
SELECT ResourceGroupBase.Name AS ResourceGroupName,
TeamBase.Name AS TeamName
FROM ResourceGroupBase
INNER JOIN ResourceGroupToTeam
ON ResourceGroupBase.ResourceGroupId = ResourceGroupToTeam.ConstraintBasedGroupId
INNER JOIN TeamBase
ON ResourceGroupToTeam.TeamId = TeamBase.TeamId



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




phual
phual
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 163
That works!

Thanks to all involved.

Stuart
hiszorn
hiszorn
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 4
I have run into similar issues regarding this table and extracting information out of the constraints column. I attempted to run your code samples but they do not run. Do you happen to have a full code sample that you can supply?

Basically I just want to extract the ID's out of that constraints column for a specific row on the ConstraintBasedGroupBase table and put them into another table so I can select against them. Why in the world they store the resource Id's like this is beyond me!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search