This question already has an answer here:
Query Data from XML
(1 answer)
Closed 5 years ago.
I have a table in an Oracle 12c Database that contains a XML Type Column and i need to extract a value from this Column.
Here an example of this Column's content:
<AbcMsg xmlns="http://example.org/SCL/CommonTypes">
<Msg>
<Pmnt>
<swif:SWIFT MT="202" xmlns:swif="urn:abc/scl/SWIFT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<swif:Header>
<swif:Input_Output_Identifier>O</swif:Input_Output_Identifier>
<swif:Sender_BIC>XXXXXXXXXXX</swif:Sender_BIC>
<swif:Receiver_BIC>XXXXXXXXXXX</swif:Receiver_BIC>
<swif:Message_Priority>NORM</swif:Message_Priority>
<swif:User_Header>
<swif:Message_User_Reference>XXXXXXXXXXXXXXXXX</swif:Message_User_Reference>
</swif:User_Header>
<swif:Original_Swift>
<swif:Message>DATA TO BE EXTRACTED</swif:Message>
</swif:Original_Swift>
<swif:Create_Date_Time>2016-07-28T15:45:00</swif:Create_Date_Time>
<swif:Msg_ID>POFu7yCXHoN</swif:Msg_ID>
<swif:Sequence_Number>248600</swif:Sequence_Number>
<swif:Session_Number>6184</swif:Session_Number>
</swif:Header>
<swif:Data xsi:type="swif:Data_202">
<swif:F20>
<swif:Sender_s_Reference>POFu7yCXHoN</swif:Sender_s_Reference>
</swif:F20>
<swif:F21>
<swif:Related_Reference>XXXXXXXXXXX</swif:Related_Reference>
</swif:F21>
<swif:F32A>
<swif:Currency>USD</swif:Currency>
<swif:Amount>156020000</swif:Amount>
<swif:Date>2016-07-28</swif:Date>
</swif:F32A>
<swif:Cdrt_F58a_F59a>
<swif:Account>174208531</swif:Account>
<swif:Identifier_Code>XXXXXXXXX</swif:Identifier_Code>
</swif:Cdrt_F58a_F59a>
</swif:Data>
</swif:SWIFT>
</Pmnt>
<Extn/>
</Msg>
</AbcMsg>
I need to retrieve the Value of <swif:Original_Swift>. I've tried the function EXTRACT(xml_column, '/AbcMsg/Msg/Pmnt') but it keeps returning null.
Any clue on how to do that???
Thanks in Advance.
Try xmltable or xmlquery. You have to declare defult and swif namesapce;
select * from xmltable(XMLNAMESPACES('urn:abc/scl/SWIFT' as "swif", default 'http://example.org/SCL/CommonTypes') , '/AbcMsg/Msg/Pmnt/swif:SWIFT/swif:Header/swif:Original_Swift' passing xmltype(your_xml_here));
Related
This question already has answers here:
SQL Server Xml query with multiple namespaces
(3 answers)
Closed 3 years ago.
The problem
I need to take data from XML files provided by another application. These XMLs contain data that is required to be inserted into tables for a database for a different application.
The problem is this, I can't get the values out of the XML file. I keep getting blank/null values instead.
I am using SQL Server 2014 Management Studio.
What I've tried
I've looked into how to read an XML and have that placed into a table. I've gone as far as looking at tutorials that work. But, whenever I try to apply the same process to my tables and using my XML data I keep getting blank values.
Some help would be appreciated as I have been looking into this for a day and researching how to approach this using MS SQL. And I cannot for the life of me understand why I keep getting nulls.
The XML
<DistributionOrder xmlns="a-url">
<Date>2019-03-07T14:38:00</Date>
<Order_Number>ACME01</Order_Number>
<Comment/>
<Status>Active</Status>
<Lines>
<Line>
<Order_Line_Number>1</Order_Line_Number>
<Product>22</Product>
<Status_Line>Active</Status_Line>
<Comment_Line/>
<Location>ENT_78</Location>
</Line>
</Lines>
</DistributionOrder>
The query
DECLARE #xmlData XML;
INSERT INTO dbo.XMLwithOpenXML(XML_data)
SELECT *
FROM OPENROWSET(BULK 'C:\test_file.xml', SINGLE_BLOB) AS ImportSource;
SELECT * FROM XMLwithOpenXML;
SELECT
element.value('(Order_Line_Number/text())[1]', 'nvarchar(80)') AS Order_Line_Number,
element.value('(Product/text())[1]', 'nvarchar(80)') AS Product
FROM
XMLwithOpenXML
CROSS APPLY
XML_data.nodes('DistributionOrder/Lines/Line') AS DO(element)
GO
What I expect
Order_Line_Number | Product
------------------+----------
1 | 22
What I Get
Order_Line_Number | Product
------------------+----------
|
Figured out that the,
<DistributionOrder xmlns="a-url">
</DistributionOrder>
was the problem. I deleted the xmlns="a-url" from the xml and found that my code is working.
Discovered that you can use
;WITH XMLNAMESPACES(DEFAULT 'a-url')
To take the namespace into account.
This question already has an answer here:
How can we separate arabic names from a column in SQL Server 2012?
(1 answer)
Closed 4 years ago.
I'm working in SSMS and SQL.
I have a table that contains lines with Arabic letters. I'd like to filter those specific lines.
I figure there has to be a way to do it with regex somehow but I can't find any examples that show how to do it exactly.
Can anyone please assist?
This was my first attempt at this. I might have messed up the syntax. Please correct me if so.
A.Text like N'[ـا اـب ـبـ بـ بـت ـتـ تـ تـث ـثـ ثـ ثـج ـجـ جـ جـح ـحـ حـ حـخ ـخـ خـ خـد دـذ ذـر رـز زـس ـسـ سـ سـش ـشـ شـ شـص ـصـ صـ صـض ـضـ ضـ ضـط ـطـ طـ طـظ ـظـ ظـ ظـع ـعـ عـ عـغ ـغـ غـ غـف ـفـ فـ فـق ـقـ قـ قـك ـكـ كـ كـل ـلـ لـ لـم ـمـ مـ مـن ـنـ نـ نـه ـهـ هـ هـو وـي ـيـ يـ يـأ أـإ إـؤ ؤـئ ـئـ ئـ ئـآ آ ]'
This didn't work. It returned 2 results instead of thousands. I don't speak arabic and I don't know what it returned exactly so its not an option to compare.
try this:
select *
from tbl
where arabic_field <> convert(varchar(8000), arabicfield) --this may be omitted
and arabic-field like '%['+nchar(0x0600)+'-'+nchar(0x06FF)+']%' --checks if contains at least an arabic char (unicode 0600–06FF)
This question already has answers here:
SQL: How can I get the value of an attribute in XML datatype?
(4 answers)
Closed 4 years ago.
how can i find and split a value between double quotation
Value is like this :
'<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
value is one filed of table and i want split "EC,ETC,Val" value
i mean 180,711,1679
thanks for your help
You Can try the following
DECLARE #XMLData XML = '<Relations mfrid="EnvoeyName_MFR"><Form EC="180" ETC="711" Val="1679" /></Relations>'
SELECT
EC = Node.Data.value('#EC', 'INT'),
ETC = Node.Data.value('#ETC', 'INT'),
Val = Node.Data.value('#EC', 'INT')
FROM #XMLData.nodes('/Relations/Form') Node(Data)
I am trying to parse an XML field using SQL into a table and I need a little help starting. An example of the XML field for one row is as follows:
<MarketValueTransactionVo
objectId="104" statusCode="0" acctNum="60835733" recType="6"
errorFlag="N" sourceCode="0" userId="DATAEXCHANGE" taxItem="0"
amount="4496.79" accountEntityType="0" transactionAmount="4496.79"
importFormatType="5" dateEntered="01252015" clearingFirmBrokerCode="OPSX"
formattedAmount="$4,496.79" totalShares="0" controlNumberSequence="0"
applicableYear="2014" brokerCode="OPSX" ssn="632248334"
entityId="OPSX" entityTypeCode="4" activityApplicationCode="3001"
activityTypeCode="801" entityPresentationNumber="0" checkStatusCode="0"
correctionCode="0" correctionTypeCode="0" entityLOBCode="0"
requestPresentationNumber="0" requestStatusCode="0" reverseReasonCode="0"
loanPresentationNumber="0">
</MarketValueTransactionVo>
You want to address XML tag attributes. Tag attributes can be addressed using at-sign symbol #, see for examples Import XML with Attribute to SQL Server Table and Convert Xml to Table SQL Server (the second format in the answer):
SELECT
Tbl.Col.value('#objectId', 'int'),
Tbl.Col.value('#statusCode', 'tinyint'),
Tbl.Col.value('#acctNum', ...proper type int? varchar(xx)? ),
...
FROM #xml.nodes('//MarketValueTransactionVo') Tbl(Col)
This question already has answers here:
TSQL 2005, XML DML - Update Two Values at once?
(3 answers)
Updating multiple XML nodes using T-SQL [duplicate]
(1 answer)
Closed 8 years ago.
I'm using SQL Server and have Table with XML column. My procedure for insert has input parameter XML document. Is there any way how could I update already existing XML in my table?
This is my OLD XML in my table:
<weather Location="Paris, France">
<forecast>
<description>Sky is clear</description>
<Date>2013-09-19</Date>
<MinTemp>13</MinTemp>
<MaxTemp>20</MaxTemp>
<Humidity>78</Humidity>
<Pressure>1024</Pressure>
<Windspeed>3</WindSpeed>
</forecast>
<forecast>
<description>Sky is clear</description>
<Date>2013-09-20</Date>
<MinTemp>14</MinTemp>
<MaxTemp>21</MaxTemp>
<Humidity>75</Humidity>
<Pressure>1020</Pressure>
<Windspeed>1</WindSpeed>
</forecast> .... 10 times this forecast
</weather>
This is my new one:
<weather Location="Paris, France">
<forecast>
<description>Sky is clear</description>
<Date>2013-09-19</Date>
<MinTemp>14</MinTemp>
<MaxTemp>21</MaxTemp>
<Humidity>70</Humidity>
<Pressure>1000</Pressure>
<Windspeed>5</WindSpeed>
</forecast>
<forecast>
<description>Sky is clear</description>
<Date>2013-09-20</Date>
<MinTemp>17</MinTemp>
<MaxTemp>24</MaxTemp>
<Humidity>68</Humidity>
<Pressure>1024</Pressure>
<Windspeed>3</WindSpeed>
</forecast> .... 10 times this forecast
</weather>
What I want now is to update my old XML with values I read from new one. I know I have to somehow go trough my OLD XML, I think I could do that with ...while(exist) and modify, but how to read value I need from my new XML, how to read let's say data for September 19th, and update September 19th in old XML, then read data for September 20th, then update September 20th in old XML etc etc...?
Thanks for help...
You may try likethis:-
update tbl1
set myXml.modify('replace value of (/weather/forecast/..)[1]
with concat(string(sql:column("columnname")), "value")')
where myXml.exist('/weather/forecast/..') = 1 //some condition