Solved

Extract data from a field that looks like XML but the data type is actually nvarchar

Posted on 2016-09-29
7
32 Views
Last Modified: 2016-11-09
Hi there,
We're currently upgrading/migrating a DB where data is polled every hour. The old DB had individual columns (ie. DateTime, Pressure, Temp, etc.), however, for the new one, we found out they captured everything in one column, and formatted as XML.

I tried using SELECT col.value('(/Data/Fields[Name="XXX"]/Value)[1]', 'int'), however after digging around the FieldData column is actually nvarchar, so that didn't work.

I'm trying to figure out how to extract parts of the data within the FieldData column (see highlighted).

Any questions or further info, please let me know...

Much appreciated,
Classic
Output.jpg
0
Comment
Question by:Classic1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 41821887
Hi Classic1,
Did you try casting the field as XML?
0
 

Author Comment

by:Classic1
ID: 41822580
Lol...yup, that helped...ok, I was able to cast it as XML, however, when I try to select say Name="000", I get an error (see attached), I made a typo, should be: ('(/Data/Fields/[Name="000"]/Value)[1])
but that didn't do the trick either.

I would like to select the RV's that are highlighted, but I don't know how to create the syntax...first time working with XML.

Much appreciated,
Classic
XML-Output.jpg
XML-Error.jpg
0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 500 total points
ID: 41823198
Hi Classic1,
Please try similar to the following:

SELECT
....
....
Field_Data.value('(/Fields/[@Name="000"]/Value)[1]', 'int')

Open in new window


that is to say, prefix 'Name' with @ symbol, as in XML terminology, in your XML output, Name is an attribute
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Classic1
ID: 41823738
Hi Barry,
Thanks for the quick response, unfortunately, that didn't work (same error), but I got at little further...and now know the difference between node attributes and elements!
I took out the [] and got a different error. After changing the data type to char(5), it actually ran. It returns a value of 'false', so I don't know what it's actually bringing back...

I've attached some screenshots for reference...

Much appreciated,
Classic1
Error.jpg
Output.jpg
0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 500 total points
ID: 41823807
Hi Classic1
Please try

Field_Data.value('(/Data/Fields/Field/@Name="000")[1]', 'char(5)')
0
 

Author Comment

by:Classic1
ID: 41823838
Hi Barry,
Ok, it changed from 'false' to 'true'...

Thanks for the quick responses!
-Classic
Result.jpg
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question