How do I extract data from XML columns into their own columns?
I have 280,000 rows that contain a column called body
. This column has xml data.
A copy of one column value is shown below. I need to extract into new columns the data contained in the elements of the XML field esn
, timestamp_utc
, Latitude
, Longitude
, 开发者_JAVA百科and triggers
, with the column names being the same as the element names.
I have worked on this in the past, but gave up on it. I am not at a point I need to figure out how to do it. Please give me any suggestions you may have.
Thank you!!!
<?xml version="1.0" encoding="UTF-8"?>
<telemetry>
<esn>1127877</esn>
<timestamp_utc>5/28/2011 9:18:30 PM</timestamp_utc>
<latitude>59.023672</latitude>
<longitude>-118.836408</longitude>
<ignition>N</ignition>
<geofence>0</geofence>
<speed>0</speed>
<heading>0</heading>
<milestraveled>0.00</milestraveled>
<triggers>Contact_State_Change, IO_1_Closed, No_Vibration_Detected, </triggers>
<miscellaneous>Standard3</miscellaneous>
<contact1counts>0</contact1counts>
<contact2counts>0</contact2counts>
<io1state>0</io1state>
<io2state>0</io2state>
</telemetry>
You can use something like this:
SELECT
ID,
body.value('(telemetry/esn)[1]', 'bigint') AS 'esn',
body.value('(telemetry/timestamp_utc)[1]', 'varchar(50)') AS 'esn',
body.value('(telemetry/longitude)[1]', 'float') AS 'longitude',
body.value('(telemetry/latitude)[1]', 'float') AS 'latitude',
body.value('(telemetry/timestamp_utc)[1]', 'varchar(50)') AS 'triggers'
FROM
dbo.YourTableNameHere
Does that work, and give you the right data??
精彩评论