x

Retrieve text from XML in SQL

I have the following code in SQL:

 declare @xfile table (xfile xml)
 insert @xfile values ('<root company="Client1">
 <setup program="prog1" support="level4">
 free text data line 1
 free text data line 2
 ...
 </setup>
 <setup program="prog2" support="level4">
 free text data line 3
 free text data line 4
 ...
 </setup>
 <setup program="prog3" support="level4">
 free text data line 5
 free text data line 6
 ...
 </setup>
 </root>')
 
 SELECT T2.Loc.query('.')
 FROM   @xfile
 CROSS APPLY xfile.nodes('/root/setup') as T2(Loc)

This returns the following data

 <setup program="prog1" support="level4">
 free text data line 1
 free text data line 2
 ...
 </setup>
 <setup program="prog2" support="level4">
 free text data line 3
 free text data line 4
 ...
 </setup>
 <setup program="prog3" support="level4">
 free text data line 5
 free text data line 6
 ...
 </setup>

What I would like is a query that returns two columns: the value of the program attribute, and the free text part of the setup section. For example:

 Program setup
 prog1 free text data line 1
  free text data line 2
  ...
 prog2 free text data line 3
  free text data line 4
  ...
 prog3 free text data line 5
  free text data line 6
  ...


Does anyone know what query I can use to get this result?

Thanks in advance.

more ▼

asked Nov 15, 2012 at 12:25 PM in Default

avatar image

xnl28
905 60 64 68

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

This might be what you're looking for:

 select
  X.SETUP.value('(@program)[1]', 'varchar(20)') as Program,
  X.SETUP.value('.', 'varchar(max)') as [FreeText]
 from
  @xfile
  CROSS APPLY xfile.nodes('/root/setup') as X(SETUP)

That returns the following:

 +---------+---------------------------------------------------+
 | Program |                     FreeText                      |
 +---------+---------------------------------------------------+
 | prog1   |  free text data line 1 free text data line 2 ...  |
 | prog2   |  free text data line 3 free text data line 4 ...  |
 | prog3   |  free text data line 5 free text data line 6 ...  |
 +---------+---------------------------------------------------+

You won't see the line breaks in SSMS (hence why FreeText gets smashed together in the output results), but you do get the two columns you want.

more ▼

answered Nov 15, 2012 at 12:50 PM

avatar image

Kevin Feasel
6.2k 4 7 15

Yes, that is exactly what I needed. Works perfectly. Many thanks!

Nov 15, 2012 at 02:28 PM xnl28
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x738
x186

asked: Nov 15, 2012 at 12:25 PM

Seen: 844 times

Last Updated: Nov 15, 2012 at 02:28 PM

Copyright 2016 Redgate Software. Privacy Policy