|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Parse XML within my T-SQL?I have some XML stored as a string in my SQL Server 2005 Database. I want to extract the LGD field below as part of my query. How can I do this in T-SQL? <Calculation> <Result> <EngineInfo> <ModelDataVersion>14</ModelDataVersion> </EngineInfo> <Output> <EAD>2260586.523</EAD> <LGD>0.018</LGD> </Output> </Result> </Calculation> Assuming that you store data in a column of data type XML you can use the
code below. CREATE TABLE #Temp (col xml) INSERT #Temp (col) SELECT '<Calculation> <Result> <EngineInfo> <ModelDataVersion>14</ModelDataVersion> </EngineInfo> <Output> <EAD>2260586.523</EAD> <LGD>0.018</LGD> </Output> </Result> </Calculation>' SELECT T.c.value('LGD[1]','decimal(8,3)') FROM #Temp CROSS APPLY col.nodes('/Calculation/Result/Output') AS T(c) If somehow you store your xml data in (n)varchar or (n)text column (why would you?) you can start with casting this column to xml data type. Show quoteHide quote U¿ytkownik "C" <C@discussions.microsoft.com> napisa³ w wiadomo¶ci news:B8977E0B-7AF5-4872-9DDE-E4A836DEB68D@microsoft.com... > Hi, > > I have some XML stored as a string in my SQL Server 2005 Database. > > I want to extract the LGD field below as part of my query. > > How can I do this in T-SQL? > > <Calculation> > <Result> > <EngineInfo> > <ModelDataVersion>14</ModelDataVersion> > </EngineInfo> > <Output> > <EAD>2260586.523</EAD> > <LGD>0.018</LGD> > </Output> > </Result> > </Calculation> > "Pawel Potasinski" <pawel.potasin***@gmail.com> wrote in message ....Because regulatory requirements might demand that you store an exact news:%23NDd$D2$HHA.3716@TK2MSFTNGP03.phx.gbl... > If somehow you store your xml data in (n)varchar or (n)text column (why > would you?) ... character for character copy of all XML data received... It might be easier to use the value method:
DECLARE @Data XML; SET @Data = '<Calculation> <Result> <EngineInfo> <ModelDataVersion>14</ModelDataVersion> </EngineInfo> <Output> <EAD>2260586.523</EAD> <LGD>0.018</LGD> </Output> </Result> </Calculation>'; SELECT @Data.value('(/*/Result/Output/LGD)[1]', 'NVARCHAR(10)') LGD; Show quoteHide quote "Pawel Potasinski" <pawel.potasin***@gmail.com> wrote in message news:%23NDd$D2$HHA.3716@TK2MSFTNGP03.phx.gbl... > Assuming that you store data in a column of data type XML you can use the > code below. > > CREATE TABLE #Temp (col xml) > > INSERT #Temp (col) > SELECT '<Calculation> > <Result> > <EngineInfo> > <ModelDataVersion>14</ModelDataVersion> > </EngineInfo> > <Output> > <EAD>2260586.523</EAD> > <LGD>0.018</LGD> > </Output> > </Result> > </Calculation>' > > SELECT T.c.value('LGD[1]','decimal(8,3)') > FROM #Temp > CROSS APPLY col.nodes('/Calculation/Result/Output') AS T(c) > > If somehow you store your xml data in (n)varchar or (n)text column (why > would you?) you can start with casting this column to xml data type. > > -- > Regards > Pawel Potasinski > [http://www.potasinski.pl] > > > U¿ytkownik "C" <C@discussions.microsoft.com> napisa³ w wiadomo¶ci > news:B8977E0B-7AF5-4872-9DDE-E4A836DEB68D@microsoft.com... >> Hi, >> >> I have some XML stored as a string in my SQL Server 2005 Database. >> >> I want to extract the LGD field below as part of my query. >> >> How can I do this in T-SQL? >> >> <Calculation> >> <Result> >> <EngineInfo> >> <ModelDataVersion>14</ModelDataVersion> >> </EngineInfo> >> <Output> >> <EAD>2260586.523</EAD> >> <LGD>0.018</LGD> >> </Output> >> </Result> >> </Calculation> >> > >
Found operand of type 'xdt:anyAtomicType *'. Why?!?!
need help with xquery value clause Insert data from XML file into MSSQL 2005 ... what's wrong? Newbie help with sql xml query Selecting elements of derived types in SQL Server 2005 XQuery Need help with xquery order how to get table result from mixed source? How to check the Xml value is or not conform the specific xml schema? transfer xml between sql server and .net CLR Where to start? |
|||||||||||||||||||||||