SQL Report Builder pull one node from an Xml Data Field - sql
I have a SQL table called: tblDataPermit
One column contains XML Data and is called: XmlDataField
In SQL Report Builder I am trying pull the data from the Text1Size50 node as shown in the sample xml data below. In this case it would be "Kirby Vacuum"
If I use:
SELECT
tblDataPermit.XmlDataField
FROM
tblDataPermit
It will produce all content in the XmlDataField as shown below. I only want it to show "Kirby Vacuum".
I have also tried this:
SELECT tblDataPermit.XmlDataField.value('(/Text1Size50/#Text1Size50)[1]', 'VARCHAR(50)') AS Result from dbo.tblDataPermit
WHERE ApplicationNumber = '00000002'
ORDER BY Result desc
I get a NULL value.
Here is a sample of the Xml data field:
--<XmlDataField xmlns="MgmsSchema/Business_Information_Detail_Entry" ApplicationNumber="00000865">
<Text1Size50>Kirby Vacuum</Text1Size50>
<Text2Size50>Lowes</Text2Size50>
</XmlDataField>--
Related
Transforming JSON data to relational data
I want to display data from SQL Server where the data is in JSON format. But when the select process, the data does not appear: id item_pieces_list 0 [{"id":2,"satuan":"BOX","isi":1,"aktif":true},{"id":4,"satuan":"BOX10","isi":1,"aktif":true}] 1 [{"id":0,"satuan":"AMPUL","isi":1,"aktif":"true"},{"id":4,"satuan":"BOX10","isi":5,"aktif":true}] I've written a query like this, but nothing appears. Can anyone help? Query : SELECT id, JSON_Value(item_pieces_list, '$.satuan') AS Name FROM [cisea.bamedika.co.id-hisys].dbo.medicine_alkes AS medicalkes
Your Path is wrong. Your JSON is an array, and you are trying to retrieve it as a flat object SELECT id, JSON_Value(item_pieces_list,'$[0].satuan') AS Name FROM [cisea.bamedika.co.id-hisys].dbo.medicine_alkes Only in the case of data without the [] (array sign) you could use your original query '$.satuan', but since you are using an array I change it to retrieve only the first element in the array '$[0].satuan'
How to update an existing XML field data in SQL Server table?
I am trying to update CertificateIssuers field shown in the XML output below Query: SELECT Settings FROM Sites Output: <ClientOperationalSettings> <Version>5.00.8740.1002</Version> <SecurityConfiguration> <SecurityModeMask>0</SecurityModeMask> <SecurityModeMaskEx>480</SecurityModeMaskEx> <HTTPPort>80</HTTPPort> <HTTPSPort>443</HTTPSPort> <CertificateStoreName/> <CertificateIssuers>CN=Mars-M-CA-CA-1; DC=Mars; DC=Local</CertificateIssuers> <CertificateSelectionCriteria>SubjectAttr:OU=London</CertificateSelectionCriteria> <CertificateSelectFirstFlag>1</CertificateSelectFirstFlag> <PKICertOptions>1</PKICertOptions> <SiteSigningCert>308202ED308201D5A0030201020210129F196B3060FF94418BEA860E8DD712300D06092A864886F70D01010B05003016311430120603550403130B53697465205365727665723020170D3138303933303031353534395A180F32313138303930373031353534395A3016311430120603550403130B536974652053657276657230820122300D06092A864886F70D01010105000382010F003082010A0282010100B2B3E6757D8E4DF7977F061543CF629B9129C337D373945A4FFE4625D557370FA21B6E03C09AE692A32A6B783BE2C19E3FEC965054F9A54BFDDB7C4B23E45456353EEA9D14FCA5EF58DCDECA3EEB9B7D589162DD87005799D473BB1518398C638217986780CCE8DFB778A84915019700BAF53AB3205730060911D7EEF17C4EDE904953EAEB8BAEBC85CAAFE82F121FC16A9316DB09E94D76A01F61A2129F4F807ADCB68270410393D9BD2E435C253683B013677EFFEBF09EE6BAC25D9AA21742142A58A1E2EAF09BD75C07BA18B85080F6700245461734AB9A466B56BE7D7F2F8E07D62AA75253B8A2839777E7C0A6DC6FF575164232EB05B3C9EA2CFC3531C30203010001A3353033301B0603551D110414301282104D2D5030312E4D6172732E4C6F63616C30140603551D25040D300B06092B060104018237650B300D06092A864886F70D01010B050003820101004AAAD78752D9650694401B43191B0BD586B48A96990C68B40673444C878032CDB7E8113E7344E7FD47DF18FF0DE51F8396B5F670AA5998BE34E6F91BAA79593D5578FB54A3EDABA20E14FE4B5D2736E5FB234F4FED7BA53972A5462682B67A1B3DD6FD2599DBFF0DE49B99C44C1003932816784F06D47C69584DA61C9E026EB2B1730920A06B9296FE69141CCCA1677A52AF644E5834D429F4810340FB6FB30B061A3555F4C3E506B960496CF10E54145384B1A6FE87A1FB26EB078246D132DD509F82180B0CDD9AF485329159F2145CBA6B55A36777558F2901B30B9974739640C4B8DCDF0319C9A10EEF4EAA33B74E7F54CE5C6F313DCB4FF7EEA1FA6453A5</SiteSigningCert> </SecurityConfiguration> <AADConfig Version="1.0"> <Tenants/> </AADConfig> </ClientOperationalSettings> I found XML from SQL column: Cannot call methods on nvarchar(max) and I am able to query the value like below Query: SELECT cast([Sites].[Settings] as xml).query('data(ClientOperationalSettings/SecurityConfiguration/CertificateIssuers)') FROM [dbo].[Sites] Output: (No column name) CN=Mars-M-CA-CA-1; DC=Mars; DC=Local but how to I update the CN to lets say to below for example <CertificateIssuers>CN=Mars Planet; DC=Mars; DC=Local</CertificateIssuers>
You can try the following query. UPDATE dbo.Sites SET Settings.modify('replace value of (/ClientOperationalSettings/SecurityConfiguration/CertificateIssuers/text())[1] with "CN=Mars Planet; DC=Mars; DC=Local"') WHERE ....
Create a query on unstructured XML data type in SQL Server
I have a column that holds xml data in SQL Server. I have 2 values in my column, the data looks like this: <StoreSurvey> <BankName>United Security</BankName> <BusinessType>BM</BusinessType> </StoreSurvey> and this : <StoreSurvey> <AnnualSales>2000</AnnualSales> <BankName>United Security</BankName> <BusinessType>BM</BusinessType> </StoreSurvey> As you can see these two values has a different on AnnualSales. How can I write a query to return the records with AnnualSales=2000? Sorry I am so new in XML data type.
As your is question is not clear whether you are asking to join both the result sets or filter the record where Annual sales = 2000 here is the example basing on assumed data Declare #XmlStr XML; SET #XmlStr='<StoreSurvey> <AnnualSales>2000</AnnualSales> <BankName>United Security</BankName> <BusinessType>BM</BusinessType> </StoreSurvey> <StoreSurvey> <BankName>United Security</BankName> <BusinessType>BM</BusinessType> </StoreSurvey>'; Script where you can only get Annual sales = 2000 SELECT [Table].[Column].value('AnnualSales[1]', 'int') as 'AnnualSales ', [Table].[Column].value('BankName[1]', 'varchar(20)') as 'BankName', [Table].[Column].value('BusinessType[1]', 'varchar(50)') as 'BusinessType' FROM #XmlStr.nodes('/StoreSurvey') as [Table]([Column]) WHERE [Table].[Column].value('(AnnualSales[1])[1]', 'int') = 2000
Issue to generate dynamic xml with XMLElement
I have a query: (show next below): SELECT XMLElement("PetitionMarreCSV", XMLAttributes('http://INT010.GPA.Schemas.External.GPA_AllFile' AS "xmlns"), XMLForest(EVENT as "Event",INSERTDATE as "InsertDate",USER_ID as "USER_ID",FIRSTNAME as "FirstName",NAME as "Name",EMAIL as "Email",LANGUAGE as "Language",COUNTRY as "COUNTRY",USER_PROFILE_PIC as "USER_PROFILE_PIC",USER_PROFILE_VIDEO as "USER_PROFILE_VIDEO",OPTIN as "OPTIN",USER_WISTIA_VIDEO as "USER_WISTIA_VIDEO",USER_ACEPT as "USER_ACEPT",USER_APROVED as "USER_APROVED",STRET as "STRET",CITY as "City",POSTALCODE as "PostalCode",TELEPHONE as "Telephone",USER_INTERESTS as "USER_INTERESTS",USER_VIDEO_VIEWS as "USER_VIDEO_VIEWS",USER_SORT_ORDER as "USER_SORT_ORDER",DALING_VAN_DE_KOPKRACHT as "DALING_VAN_DE_KOPKRACHT",TOEGANG_TOT_DE_GEZONDHEIDSZORG as "TOEGANG_TOT_DE_GEZONDHEIDSZORG",RISICO_OP_EN_BLACK_OUT as "RISICO_OP_EN_BLACK_OUT",GEPLANDE_VEROUDERING as "GEPLANDE_VEROUDERING",VERHOGING_VERZEKERINGSPRIJZEN as "VERHOGING_VERZEKERINGSPRIJZEN",DURE_INTERNET as "DURE_INTERNET",TREINVERTRAGINGEN as "TREINVERTRAGINGEN",TRAGHEID_VAN_JUSTITIE as "TRAGHEID_VAN_JUSTITIE",LAGE_RENDEMENT_SPAREKENING as "LAGE_RENDEMENT_SPAREKENING",ONGEZONDE_JUNKFOD as "ONGEZONDE_JUNKFOD",MINDER_POSTKANTOREN as "MINDER_POSTKANTOREN",OPLICHTERIJ_EN_BEDROG as "OPLICHTERIJ_EN_BEDROG",VERKOPERS_AN_HUIS as "VERKOPERS_AN_HUIS",FILENAME_SOURCE as "FileName_Source")) AS "RESULT" FROM (SELECT EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE FROM ops$kli.GPA22_20150130 GROUP BY EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE) This query create a list of CLOB value in xml format. But, by some reason that I don't know, sometimes retrieve the next error for some CLOB result instead to show the xml value: Error: XML document must have a top level element. The strange thing is, if I retrieve (filter the query) only with the row who has the error instead of multiples rows, the value with the error is not anymore. SELECT XMLElement("PetitionMarreCSV", XMLAttributes('http://INT010.GPA.Schemas.External.GPA_AllFile' AS "xmlns"), XMLForest(EVENT as "Event",INSERTDATE as "InsertDate",USER_ID as "USER_ID",FIRSTNAME as "FirstName",NAME as "Name",EMAIL as "Email",LANGUAGE as "Language",COUNTRY as "COUNTRY",USER_PROFILE_PIC as "USER_PROFILE_PIC",USER_PROFILE_VIDEO as "USER_PROFILE_VIDEO",OPTIN as "OPTIN",USER_WISTIA_VIDEO as "USER_WISTIA_VIDEO",USER_ACEPT as "USER_ACEPT",USER_APROVED as "USER_APROVED",STRET as "STRET",CITY as "City",POSTALCODE as "PostalCode",TELEPHONE as "Telephone",USER_INTERESTS as "USER_INTERESTS",USER_VIDEO_VIEWS as "USER_VIDEO_VIEWS",USER_SORT_ORDER as "USER_SORT_ORDER",DALING_VAN_DE_KOPKRACHT as "DALING_VAN_DE_KOPKRACHT",TOEGANG_TOT_DE_GEZONDHEIDSZORG as "TOEGANG_TOT_DE_GEZONDHEIDSZORG",RISICO_OP_EN_BLACK_OUT as "RISICO_OP_EN_BLACK_OUT",GEPLANDE_VEROUDERING as "GEPLANDE_VEROUDERING",VERHOGING_VERZEKERINGSPRIJZEN as "VERHOGING_VERZEKERINGSPRIJZEN",DURE_INTERNET as "DURE_INTERNET",TREINVERTRAGINGEN as "TREINVERTRAGINGEN",TRAGHEID_VAN_JUSTITIE as "TRAGHEID_VAN_JUSTITIE",LAGE_RENDEMENT_SPAREKENING as "LAGE_RENDEMENT_SPAREKENING",ONGEZONDE_JUNKFOD as "ONGEZONDE_JUNKFOD",MINDER_POSTKANTOREN as "MINDER_POSTKANTOREN",OPLICHTERIJ_EN_BEDROG as "OPLICHTERIJ_EN_BEDROG",VERKOPERS_AN_HUIS as "VERKOPERS_AN_HUIS",FILENAME_SOURCE as "FileName_Source")) AS "RESULT" FROM (SELECT EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE FROM ops$kli.GPA22_20150130 WHERE user_id = 293 GROUP BY EVENT,INSERTDATE,USER_ID,FIRSTNAME,NAME,EMAIL,LANGUAGE,COUNTRY,USER_PROFILE_PIC,USER_PROFILE_VIDEO,OPTIN,USER_WISTIA_VIDEO,USER_ACEPT,USER_APROVED,STRET,CITY,POSTALCODE,TELEPHONE,USER_INTERESTS,USER_VIDEO_VIEWS,USER_SORT_ORDER,DALING_VAN_DE_KOPKRACHT,TOEGANG_TOT_DE_GEZONDHEIDSZORG,RISICO_OP_EN_BLACK_OUT,GEPLANDE_VEROUDERING,VERHOGING_VERZEKERINGSPRIJZEN,DURE_INTERNET,TREINVERTRAGINGEN,TRAGHEID_VAN_JUSTITIE,LAGE_RENDEMENT_SPAREKENING,ONGEZONDE_JUNKFOD,MINDER_POSTKANTOREN,OPLICHTERIJ_EN_BEDROG,VERKOPERS_AN_HUIS,FILENAME_SOURCE) Do you know if there is a problem with this function when you try to generate several CLOB columns?
SQL Server - XQuery for XML
Just similar other post, I need to retrieve any rows from table applying criteria on Xml column, for instance, supposing you have an xml column like this: <DynamicProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WinTest"> <AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <d2p1:KeyValueOfstringstring> <d2p1:Key>One</d2p1:Key> <d2p1:Value>1</d2p1:Value> </d2p1:KeyValueOfstringstring> <d2p1:KeyValueOfstringstring> <d2p1:Key>Two</d2p1:Key> <d2p1:Value>2</d2p1:Value> </d2p1:KeyValueOfstringstring> </AllData> </DynamicProfile> My query would be able to return all rows where node value <d2p1:Key> = 'some key value' AND node value <d2p1Value = 'some value value'. Imagine of that just as a dynamic table where KEY node represent the column name and Value node represent column's value. The following query does not work because key and value nodes are not sequential: select * from MyTable where MyXmlField.exist('//d2p1:Key[.="One"]') = 1 AND MyXmlField.exist('//d2p1:Value[.="1"]') = 1
Instead of looking for //d2p1:key[.="One"] and //d2p1:Value[.="1"] as two separate searches, do a single query that looks for both at once, like so: //d2p1:KeyValueOfstringstring[./d2p1:Key="One"][./d2p1:Value=1]