Avatar of Classic1
Classic1
Flag for Canada asked on

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

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Classic1

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Barry Cunney

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Classic1

ASKER
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
SOLUTION
Barry Cunney

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Classic1

ASKER
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
SOLUTION
Barry Cunney

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Classic1

ASKER
Hi Barry,
Ok, it changed from 'false' to 'true'...

Thanks for the quick responses!
-Classic
Result.jpg
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy