﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Trying to select XML data for one tag from an text field and isn't returning the data I need / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 16:38:16 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>[quote][b]davidsalazar01 (2/12/2013)[/b][hr]I'm getting the following error when executing the following SQL:SELECT ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS DIDFROM Payment_Method_T (nolock) OUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);Msg 9506, Level 16, State 1, Line 1The XMLDT method 'nodes' can only be invoked on columns of type xml.Can you let me know what I'm doing wrong?[/quote]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: -[quote][b]Cadavre (2/12/2013)[/b][hr]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 [url=http://www.sqlservercentral.com/articles/Best+Practices/61537/]read through this link --&amp;gt; http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url], 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.[/quote]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.[quote][b]davidsalazar01 (2/12/2013)[/b][hr]Also, is this whole string required (e.g. '/div/ul/li/did')?  I only want /DID tag only.  What is the other chars for?[/quote]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.</description><pubDate>Wed, 13 Feb 2013 01:35:05 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>Cadavre,I'm getting the following error when executing the following SQL:SELECT ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS DIDFROM Payment_Method_T (nolock) OUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);Msg 9506, Level 16, State 1, Line 1The 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</description><pubDate>Tue, 12 Feb 2013 11:01:25 GMT</pubDate><dc:creator>davidsalazar01</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>From the sample data provided by Lowell: -[code="sql"]-- SAMPLE DATAIF object_id('tempdb..#yourTable') IS NOT NULLBEGIN    DROP TABLE #yourTable;END;SELECT id, CAST(XMLProfile_TXT AS XML) AS XMLProfile_TXTINTO #yourTableFROM (VALUES(1, '&amp;lt;div class="MainMenu"&amp;gt;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Home&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Tags&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Articles&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Editorials&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Stairways&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Forums&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Scripts&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Videos&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Blogs&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;QotD&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Books/"	data-event-category="MainMenu: Books"&amp;gt;Books&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="http://ask.sqlservercentral.com"	data-event-category="MainMenu: Ask SSC"&amp;gt;Ask SSC&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="http://jobs.sqlservercentral.com/"	data-event-category="MainMenu: SQL Jobs"&amp;gt;SQL Jobs&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Training/"	data-event-category="MainMenu: Training"&amp;gt;Training&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Authors/Articles/"	data-event-category="MainMenu: Authors"&amp;gt;Authors&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="ssc_showPostsSinceLastVisit()"	data-event-category="MainMenu: Active Threads"&amp;gt;Active Threads&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/About/AboutUs/"	data-event-category="MainMenu: About us"&amp;gt;About us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/About/ContactUs/"	data-event-category="MainMenu: Contact us"&amp;gt;Contact us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/NewsletterArchive"	data-event-category="MainMenu: Newsletters"&amp;gt;Newsletters&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/Contributions/Home"	data-event-category="MainMenu: Write for us"&amp;gt;Write for us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&amp;lt;/div&amp;gt;')      )a(id, XMLProfile_TXT);[/code]This is how I'd do it using XML.[code="sql"]SELECT id, ParamValues.XMLProfile_TXT.value('.','VARCHAR(MAX)') AS didFROM #yourTableOUTER APPLY XMLProfile_TXT.nodes('/div/ul/li/did') ParamValues(XMLProfile_TXT);[/code]Returns: -[code="plain"]id          did----------- ---------------1           Home1           Tags1           Articles1           Editorials1           Stairways1           Forums1           Scripts1           Videos1           Blogs1           QotD1           Books1           Ask SSC1           SQL Jobs1           Training1           Authors1           Active Threads1           About us1           Contact us1           Newsletters1           Write for us[/code]</description><pubDate>Tue, 12 Feb 2013 07:15:31 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>[quote][b]davidsalazar01 (2/12/2013)[/b][hr]Lowell,It's only one occurence (e.g.&amp;lt;DID&amp;gt;) 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[/quote]yes it would do it, and is prepared for the other things i mentioned as well: multiple DID tags, and attributes in the opening &amp;lt;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.[b][url=http://www.sqlservercentral.com/articles/72993/]Tally OH! An Improved SQL 8K “CSV Splitter” Function[/url][/b]</description><pubDate>Tue, 12 Feb 2013 06:03:34 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>Lowell,It's only one occurence (e.g.&amp;lt;DID&amp;gt;) 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</description><pubDate>Tue, 12 Feb 2013 05:58:04 GMT</pubDate><dc:creator>davidsalazar01</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>david the select statement you posted will find where the END tag begins...wouldn't you want the value between the &amp;lt;did&amp;gt; &amp;lt;/did&amp;gt; tags?could the opening &amp;lt;did tag have attributes?is there more than one &amp;lt;did tag in the xml?this is how i would do it, using the function DelimitedSplit8K(search SSC for the article)[code]With table_name (ID,XMLProfile_TXT)AS(SELECT 1,'&amp;lt;div class="MainMenu"&amp;gt;&amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Home&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Tags&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Articles&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Editorials&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;&amp;gt;Stairways&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Forums&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Scripts&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Videos&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Blogs&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;QotD&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Books/"	data-event-category="MainMenu: Books"&amp;gt;Books&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="http://ask.sqlservercentral.com"	data-event-category="MainMenu: Ask SSC"&amp;gt;Ask SSC&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="http://jobs.sqlservercentral.com/"	data-event-category="MainMenu: SQL Jobs"&amp;gt;SQL Jobs&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Training/"	data-event-category="MainMenu: Training"&amp;gt;Training&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem "	href="/Authors/Articles/"	data-event-category="MainMenu: Authors"&amp;gt;Authors&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="ssc_showPostsSinceLastVisit()"	data-event-category="MainMenu: Active Threads"&amp;gt;Active Threads&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/About/AboutUs/"	data-event-category="MainMenu: About us"&amp;gt;About us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/About/ContactUs/"	data-event-category="MainMenu: Contact us"&amp;gt;Contact us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/NewsletterArchive"	data-event-category="MainMenu: Newsletters"&amp;gt;Newsletters&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-clicks MenuItem  Minor"	href="/Contributions/Home"	data-event-category="MainMenu: Write for us"&amp;gt;Write for us&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&amp;lt;/div&amp;gt;' UNION ALLSELECT 2, '&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Home&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Tags&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Articles&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Editorials&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;&amp;gt;Stairways&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Forums&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Scripts&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Videos&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;Blogs&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did&amp;gt;QotD&amp;lt;/did&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;did class="track-c')select   myfn.*,  table_name.*,  REPLACE(myfn.Item,'&amp;lt;/did','')from table_name CROSS APPLY dbo.DelimitedSplit8K(XMLProfile_TXT,'&amp;gt;') myfnWhere myfn.Item LIKE '%&amp;lt;/did'[/code]</description><pubDate>Tue, 12 Feb 2013 05:33:33 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>[quote][b]davidsalazar01 (2/11/2013)[/b][hr]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 &amp;lt;/DID&amp;gt;.   When I try to use the following TSQL syntax below, I get '0' for every record returned:select charindex ('&amp;lt;/DID&amp;gt;', 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. &amp;lt;/DID&amp;gt;).Can someone help?  I'm sure this is an easy answer.  :) Thanks!-Dave[/quote]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 [url=http://www.sqlservercentral.com/articles/Best+Practices/61537/]read through this link --&amp;gt; http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url], 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.[hr][b]--EDIT--[/b]This [url=http://www.sqlservercentral.com/Forums/Topic1415340-338-1.aspx]thread (http://www.sqlservercentral.com/Forums/Topic1415340-338-1.aspx)[/url] might help.</description><pubDate>Tue, 12 Feb 2013 05:00:37 GMT</pubDate><dc:creator>Cadavre</dc:creator></item><item><title>Trying to select XML data for one tag from an text field and isn't returning the data I need</title><link>http://www.sqlservercentral.com/Forums/Topic1418692-392-1.aspx</link><description>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 &amp;lt;/DID&amp;gt;.   When I try to use the following TSQL syntax below, I get '0' for every record returned:select charindex ('&amp;lt;/DID&amp;gt;', 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. &amp;lt;/DID&amp;gt;).Can someone help?  I'm sure this is an easy answer.  :) Thanks!-Dave</description><pubDate>Mon, 11 Feb 2013 17:25:26 GMT</pubDate><dc:creator>davidsalazar01</dc:creator></item></channel></rss>