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


Trying to select XML data for one tag from an text field and isn't returning the data I need


Trying to select XML data for one tag from an text field and isn't returning the data I need

Author
Message
davidsalazar01
davidsalazar01
SSC Veteran
SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)

Group: General Forum Members
Points: 205 Visits: 358
Hello DBA's/Developers,

I'm trying to select XML data from one tag that is stored in an TEXT data type and can't seem to return the data I need.

e.g. XMLProfile_TXT field and tag </DID>.

When I try to use the following TSQL syntax below, I get '0' for every record returned:

select charindex ('</DID>', XMLProfile_TXT) from table_name

Everytime I select all the XML data I can see the data I need in the result set, but I don't want every tag, I just need the one noted above (e.g. </DID>Wink.

Can someone help? I'm sure this is an easy answer. Smile

Thanks!

-Dave



Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2594 Visits: 8437
davidsalazar01 (2/11/2013)
Hello DBA's/Developers,

I'm trying to select XML data from one tag that is stored in an TEXT data type and can't seem to return the data I need.

e.g. XMLProfile_TXT field and tag </DID>.

When I try to use the following TSQL syntax below, I get '0' for every record returned:

select charindex ('</DID>', XMLProfile_TXT) from table_name

Everytime I select all the XML data I can see the data I need in the result set, but I don't want every tag, I just need the one noted above (e.g. </DID>Wink.

Can someone help? I'm sure this is an easy answer. Smile

Thanks!

-Dave


Hello and welcome to SSC,

If you could knock up some sample data and DDL scripts, then your expected results based on the sample data provided that would be extremely useful in allowing people to help you.

Have a read through this link --> http://www.sqlservercentral.com/articles/Best+Practices/61537/, if you're unsure how best to lay this information out. Remember that if your sample data and DDL script is readily consumable, then the volunteers for this forum are much more likely to take time out from their work to give you a hand.

Thanks.


--EDIT--
This thread (http://www.sqlservercentral.com/Forums/Topic1415340-338-1.aspx) might help.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14958 Visits: 38964
david the select statement you posted will find where the END tag begins...wouldn't you want the value between the <did> </did> tags?

could the opening <did tag have attributes?

is there more than one <did tag in the xml?

this is how i would do it, using the function DelimitedSplit8K(search SSC for the article)


With table_name (ID,XMLProfile_TXT)
AS
(SELECT 1,'
<div class="MainMenu"
><ul>
<li><did>Home</did></li>
<li><did>Tags</did></li>
<li><did>Articles</did></li>
<li><did>Editorials</did></li>
<li><>Stairways</did></li>
<li><did>Forums</did></li>
<li><did>Scripts</did></li>
<li><did>Videos</did></li>
<li><did>Blogs</did></li>
<li><did>QotD</did></li>
<li><did class="track-clicks MenuItem " href="/Books/" data-event-category="MainMenu: Books">Books</did></li>
<li><did class="track-clicks MenuItem " href="http://ask.sqlservercentral.com" data-event-category="MainMenu: Ask SSC">Ask SSC</did></li>
<li><did class="track-clicks MenuItem " href="http://jobs.sqlservercentral.com/" data-event-category="MainMenu: SQL Jobs">SQL Jobs</did></li>
<li><did class="track-clicks MenuItem " href="/Training/" data-event-category="MainMenu: Training">Training</did></li>
<li><did class="track-clicks MenuItem " href="/Authors/Articles/" data-event-category="MainMenu: Authors">Authors</did></li>
<li><did class="track-clicks MenuItem Minor" href="ssc_showPostsSinceLastVisit()" data-event-category="MainMenu: Active Threads">Active Threads</did></li>
<li><did class="track-clicks MenuItem Minor" href="/About/AboutUs/" data-event-category="MainMenu: About us">About us</did></li>
<li><did class="track-clicks MenuItem Minor" href="/About/ContactUs/" data-event-category="MainMenu: Contact us">Contact us</did></li>
<li><did class="track-clicks MenuItem Minor" href="/NewsletterArchive" data-event-category="MainMenu: Newsletters">Newsletters</did></li>
<li><did class="track-clicks MenuItem Minor" href="/Contributions/Home" data-event-category="MainMenu: Write for us">Write for us</did></li>
</ul>
</div>' UNION ALL
SELECT 2, '<li><did>Home</did></li>
<li><did>Tags</did></li>
<li><did>Articles</did></li>
<li><did>Editorials</did></li>
<li><>Stairways</did></li>
<li><did>Forums</did></li>
<li><did>Scripts</did></li>
<li><did>Videos</did></li>
<li><did>Blogs</did></li>
<li><did>QotD</did></li>
<li><did class="track-c'



)
select
myfn.*,
table_name.*,
REPLACE(myfn.Item,'</did','')
from table_name
CROSS APPLY dbo.DelimitedSplit8K(XMLProfile_TXT,'>') myfn
Where myfn.Item LIKE '%</did'



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

davidsalazar01
davidsalazar01
SSC Veteran
SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)

Group: General Forum Members
Points: 205 Visits: 358
Lowell,

It's only one occurence (e.g.<DID>Wink in the XML and your right I only want the data between the tags. Would the samples you posted do the trick? I appreciate your help!

Thanks,

David



Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14958 Visits: 38964
davidsalazar01 (2/12/2013)
Lowell,

It's only one occurence (e.g.<DID>Wink in the XML and your right I only want the data between the tags. Would the samples you posted do the trick? I appreciate your help!

Thanks,

David


yes it would do it, and is prepared for the other things i mentioned as well: multiple DID tags, and attributes in the opening <did tag as well.

grab the function from this article, and read the article so you can understand what it does; it's a very powerful tool to put in your toolkit.

Tally OH! An Improved SQL 8K “CSV Splitter” Function

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2594 Visits: 8437
From the sample data provided by Lowell: -
-- SAMPLE DATA
IF object_id('tempdb..#yourTable') IS NOT NULL
BEGIN
DROP TABLE #yourTable;
END;
SELECT id, CAST(XMLProfile_TXT AS XML) AS XMLProfile_TXT
INTO #yourTable
FROM (VALUES(1, '<div class="MainMenu"><ul><li><did>Home</did></li><li><did>Tags</did></li><li><did>Articles</did></li><li><did>Editorials</did></li><li><did>Stairways</did></li>
<li><did>Forums</did></li><li><did>Scripts</did></li><li><did>Videos</did></li><li><did>Blogs</did></li><li><did>QotD</did></li>
<li><did class="track-clicks MenuItem " href="/Books/" data-event-category="MainMenu: Books">Books</did></li>
<li><did class="track-clicks MenuItem " href="http://ask.sqlservercentral.com" data-event-category="MainMenu: Ask SSC">Ask SSC</did></li>
<li><did class="track-clicks MenuItem " href="http://jobs.sqlservercentral.com/" data-event-category="MainMenu: SQL Jobs">SQL Jobs</did></li>
<li><did class="track-clicks MenuItem " href="/Training/" data-event-category="MainMenu: Training">Training</did></li>
<li><did class="track-clicks MenuItem " href="/Authors/Articles/" data-event-category="MainMenu: Authors">Authors</did></li>
<li><did class="track-clicks MenuItem Minor" href="ssc_showPostsSinceLastVisit()" data-event-category="MainMenu: Active Threads">Active Threads</did></li>
<li><did class="track-clicks MenuItem Minor" href="/About/AboutUs/" data-event-category="MainMenu: About us">About us</did></li>
<li><did class="track-clicks MenuItem Minor" href="/About/ContactUs/" data-event-category="MainMenu: Contact us">Contact us</did></li>
<li><did class="track-clicks MenuItem Minor" href="/NewsletterArchive" data-event-category="MainMenu: Newsletters">Newsletters</did></li>
<li><did class="track-clicks MenuItem Minor" href="/Contributions/Home" data-event-category="MainMenu: Write for us">Write for us</did></li></ul></div>')
)a(id, XMLProfile_TXT);



This is how I'd do it using XML.
SELECT id, ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS did
FROM #yourTable
OUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);



Returns: -
id          did
----------- ---------------
1 Home
1 Tags
1 Articles
1 Editorials
1 Stairways
1 Forums
1 Scripts
1 Videos
1 Blogs
1 QotD
1 Books
1 Ask SSC
1 SQL Jobs
1 Training
1 Authors
1 Active Threads
1 About us
1 Contact us
1 Newsletters
1 Write for us



Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
davidsalazar01
davidsalazar01
SSC Veteran
SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)

Group: General Forum Members
Points: 205 Visits: 358
Cadavre,

I'm getting the following error when executing the following SQL:

SELECT ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS DID
FROM Payment_Method_T (nolock)
OUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);

Msg 9506, Level 16, State 1, Line 1
The XMLDT method 'nodes' can only be invoked on columns of type xml.

Can you let me know what I'm doing wrong? Also, is this whole string required (e.g. '/div/ul/li/did')? I only want /DID tag only. What is the other chars for?

Thanks,
David



Cadavre
Cadavre
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2594 Visits: 8437
davidsalazar01 (2/12/2013)
I'm getting the following error when executing the following SQL:

SELECT ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS DID
FROM Payment_Method_T (nolock)
OUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);

Msg 9506, Level 16, State 1, Line 1
The XMLDT method 'nodes' can only be invoked on columns of type xml.

Can you let me know what I'm doing wrong?


I'm going to take a wild guess and say that you aren't storing it as XML, which is why I asked for DDL scripts in my first post: -
Cadavre (2/12/2013)
If you could knock up some sample data and DDL scripts, then your expected results based on the sample data provided that would be extremely useful in allowing people to help you.

Have a read through this link --> http://www.sqlservercentral.com/articles/Best+Practices/61537/, if you're unsure how best to lay this information out. Remember that if your sample data and DDL script is readily consumable, then the volunteers for this forum are much more likely to take time out from their work to give you a hand.


With that in mind, you could cast it to XML, but since I can no longer be certain that your stored data is correct XML syntax it may throw up errors so will probably be easier to use the splitter as suggested by Lowell.

davidsalazar01 (2/12/2013)
Also, is this whole string required (e.g. '/div/ul/li/did')? I only want /DID tag only. What is the other chars for?


Those other characters are required to show where the "did" tag is in relation to the XML schema. I based that on the sample data provided by Lowell because you didn't provide anything for me to work with.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
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