title: Converting a string to XML datatype before querying in T-SQL
body: <p>How do I convert a string into an XML datatype so that I can query the data as XML:</p>
  
  <p>For example (thanks to "mellamokb the Wise" who provided the original SQL for this)</p>
  
  <p>The code below works fine if xmlstring is of the type XML (see <a href="http://www.sqlfiddle.com/#!3/2e452/6" rel="nofollow"><strong>DEMO</strong></a>)</p>
  
  <pre><code>select id, name
  from Data
  cross apply (
    select Destination.value('data(@Name)', 'varchar(50)') as name
    from [xmlstring].nodes('/Holidays/Summer/Regions/Destinations/Destination') D(Destination)
  ) Destinations(Name)
  </code></pre>
  
  <p>However, if xmlString is of type varchar I get an error even though I'm converting the string to XML (<a href="http://www.sqlfiddle.com/#!3/c51e60/5" rel="nofollow"><strong>DEMO</strong></a>):</p>
  
  <pre><code>select id, name
  from Data
    cross apply (
    select Destination.value('data(@Name)', 'varchar(50)') as name
    from CONVERT(xml,[xmlstring]).nodes('/Holidays/Summer/Regions/Destinations/Destination') D(Destination)
  ) Destinations(Name)
  </code></pre>