Extract data from XML string in SQL using XQuery - sql-server

I am trying to extract data from a XML string which is stored in my table in XMLString column as below..
VId Uid Name TWd XMLString
26 jti jbreti testell string in xml format
26 Man Lomond Mcan string in xml format
26 mw mlwTest tewWell string in xml format
26 tot teapot te2Well string in xml format
XML string having below format. Its having multi level nodes also..
<well uid="b4e952f9">
<name>Demo</name>
<field>Fi Tk</field>
<country>India</country>
<county>South India</county>
<region>SiD</region>
<block>09-365</block>
<timeZone>+09:00</timeZone>
<operator>AACE Oil CO</operator>
<operatorDiv>AAACE South Australia</operatorDiv>
<statusWell>unknown</statusWell>
<wellDatum defaultElevation="true" uid="SL">
<name>Mean Sea Level</name>
<code>SL</code>
</wellDatum>
<waterDepth uom="ft">269.0256</waterDepth>
<wellLocation uid="loc-1">
<latitude uom="dega">-28.601403</latitude>
<longitude uom="dega">137.444458</longitude>
</wellLocation>
<commonData>
<dTimCreation>2012-04-10T13:17:45.959Z</dTimCreation>
<dTimLastChange>2013-11-08T14:42:56.340Z</dTimLastChange>
</commonData>
</well>
I required few nodes from above XML & this few nodes details is also in XML string as below..
<well>
<name></name>
<country></country>
<block></block>
<timeZone></timeZone>
<wellDatum>
<name></name>
<code></code>
</wellDatum>
<waterDepth></waterDepth>
</well>
Now i need to extract nodes from 1st XML with node values string that are present in 2nd XML string.
And out put should be also a XML string.
Output string should be as below....
<well uid="b4e952f9">
<name>Demo</name>
<country>India</country>
<block>09-365</block>
<timeZone>+09:00</timeZone>
<wellDatum defaultElevation="true" uid="SL">
<name>Mean Sea Level</name>
<code>SL</code>
</wellDatum>
<waterDepth uom="ft">269.0256</waterDepth>
</well>
This all i want to be done in MSSQL only.
Can anyone help me..

SELECT XMLString.query(
'<well>
<name>{/well/name/node()}</name>
<country>{/well/country/node()}</country>
<block>{/well/block/node()}</block>
<timeZone>{/well/timeZone/node()}</timeZone>
<wellDatum>
<name>{/well/wellDatum/name/node()}</name>
<code>{/well/wellDatum/code/node()}</code>
</wellDatum>
<waterDepth>{/well/waterDepth/node()}</waterDepth>
</well>'
),
XMLString FROM Well

Related

Storing binary with JDBCTemplate

I have table like
create table test(payload varbinary(max))
I am trying to store text lines in compressed format in the database using the following code
String sql = "insert into test(payload) values (compress(:payload))
MapSqlParametersource msps = new MapSqlParameterSource();
msps.addValue("payload", "some text", Types.VARBINARY)
NamedParameterJdbcTemplate npjt = //;
npjt.update(sql, msps);
This gives the following error -
String is not in a valid hex format
If I provide the datatype in MapSqlParameterSource as VARCHAR, it doesn't give any error, but then using MSSQL's decompress function returns garbage value
select decompress(payload) from test

How to convert xpath to xml with multiple nodes in SQL Server

I have a XML like this, I save the XML values in a table with value and xpath in SQL Server, as a result I have a table as below of 2nd, I need to parse XML in to path and values, then I need to gain a proper XML, I mean same XML, using path and values? I couldn't do it, how can I do this?
<Root>
<Trans>
<PaymentID>
<ID1>ID1</ID1>
<ID2>ID2</ID2>
<ID3>ID3</ID3>
<ID4>ID4</ID4>
</PaymentID>
<Amt Ccy="USD">1500</Amt>
<From>
<Inst>
<Adr>12345</Adr>
</Inst>
</From>
<To>
<Inst>
<Adr>6789</Adr>
</Inst>
</To>
<Debt>
<Inf>
<Adr>012345</Adr>
</Inf>
</Debt>
<Cred>
<Inf>
<Adr>44444</Adr>
<PstlAdr>
<AdrLine>ADR3:</AdrLine>
<AdrLine>ADR2:</AdrLine>
<AdrLine>ADR1:</AdrLine>
</PstlAdr>
</Inf>
</Cred>
</Trans>
</Root>
Table :
Root/Trans/Cred/Inf/PstlAdr/AdrLine[3] 38 ADR1: Root/Trans/Cred/Inf/PstlAdr/AdrLine
Root/Trans/Cred/Inf/PstlAdr/AdrLine[2] 37 ADR2: Root/Trans/Cred/Inf/PstlAdr/AdrLine
Root/Trans/Cred/Inf/PstlAdr/AdrLine 36 ADR3: Root/Trans/Cred/Inf/PstlAdr/AdrLine
Root/Trans/Cred/Inf/Adr 35 44444 Root/Trans/Cred/Inf/Adr
Root/Trans/Debt/Inf/Adr 34 012345 Root/Trans/Debt/Inf/Adr
Root/Trans/To/Inst/Adr 33 6789 Root/Trans/To/Inst/Adr
Root/Trans/From/Inst/Adr 32 12345 Root/Trans/From/Inst/Adr
Root/Trans/Amt/#Ccy 31 USD Root/Trans/Amt/#Ccy
Root/Trans/PaymentID/ID4 30 ID4 Root/Trans/PaymentID/ID4
Root/Trans/PaymentID/ID3 29 ID3 Root/Trans/PaymentID/ID3
Root/Trans/PaymentID/ID2 28 ID2 Root/Trans/PaymentID/ID2
Root/Trans/PaymentID/ID1 27 ID1 Root/Trans/PaymentID/ID1
Root/Trans/Amt 10 1500 Root/Trans/Amt
Then I need to convert these values to XML, but I can not convert repetitive values to XML appropriately, I use stuff and xml path and I get the result as below:
<AdrLine>ADR3:ADR2:ADR1:</AdrLine>
</PstlAdr>
How can I convert XML to xpath then xpath to XML properly?

snowflake - How to use a file format to decode a csv column?

I've got some data in a string column that is in a strange csv format. I can write a file format that correctly interprets it. How do I use my file format against data that has already been imported?
create table test_table
(
my_csv_column string
)
How do I split/flatten this column with:
create or replace file format my_csv_file_format
type = 'CSV'
RECORD_DELIMITER = '0x0A'
field_delimiter = ' '
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
VALIDATE_UTF8 = FALSE
Please assume that I cannot use split, as I want to use the rich functionality of the file format (optional escape characters, date recognition etc.).
What I'm trying to achieve is something like the below (but I cannot find how to do it)
copy into destination_Table
from
(select
s.$1
,s.$2
,s.$3
,s.$4
from test_table s
file_format = (column_name ='my_csv_column' , format_name = 'my_csv_file_format'))

Using substring to strip text from text file & insert into SQL Server database & create script text file as result

Database Specification: SQL Server 2012
Problem Statement:
I need a sql query (store procedure or function or set-base query … you the expert) to assist with processing 4.1 million records in efficient time. These records reside in a text file and therefore need to inserted into a database table. Note each record consist of the following fields at different offset values:
Herewith the offset values for only the first 6 columns...
,LTRIM(SUBSTRING([TABLE],1,13)) --National_id
,LTRIM(SUBSTRING([TABLE],14,43)) --Errmsg
,LTRIM(SUBSTRING([TABLE],57,8)) --DeceasedDTE
,LTRIM(SUBSTRING([TABLE],65,50)) --DeceasedReason
,LTRIM(SUBSTRING([TABLE],115,45)) --Surname
,LTRIM(SUBSTRING([TABLE],158,50)) --FirstNames
Note the FirstNames column could contain 3 string values separated by a space … FName1, FName2, Fname3…
This is where I’m struggling to strip this column with more than 2 string values … only because this column doesn’t work with offset values …
I have the following 3 records as a sample ... these records need to be stored in a text file and used as input..
Copy this to a text file named: Deceased.txt
000101001118 IDENTITY NUMBER NOT NUMERIC
0001010061181PERSON DECEASED 19990101OBSTRUCTIVE AIRWAYS SYNDROME BABA NOWEZILE
0001010077097 COERTZEN AZIL CUBITT JONO
Desired Result:
1. Insert each record to a sql server table.
The table will have the following columns:
National_id Errmsg DeceasedDTE DeceasedReason Surname FirstNames
First_Initial Second_Initial Third_Initial FName1 FName2 FName3
Also note the FirstNames is separated with a space and I need them spit up into each FName1, FName2 and FName3 ... with its corresponding first letter that makes up the initial..
I then need to create a script send to a .txt file that creates an insert statement for each record with the following columns... national_id, surname, First_Initial, Second_Initial,Third_Initial, First_Name, Second_Name, Third_Name
E.G.
Set #Insert = "insert into prodmgr.t_unverified (national_id, surname, First_Initial, Second_Initial,Third_Initial, First_Name, Second_Name, Third_Name) values ('"
NOTE:
DECEASED.TXT (3 RECORDS)
000101001118 IDENTITY NUMBER NOT NUMERIC
0001010061181PERSON DECEASED 19990101OBSTRUCTIVE AIRWAYS SYNDROME BABA NOWEZILE
0001010077097 COERTZEN AZIL CUBITT JONO
TABLE:DECEASED (3 RECORDS)
National_id Errmsg DeceasedDTE DeceasedReason Surname FirstNames First_Initial Second_Initial Third_Initial FName1 FName2 FName3
RESULT IN TEXT FILE: (2 RECORDS)
insert into prodmgr.t_unverified (national_id, surname, First_Initial, Second_Initial,Third_Initial, First_Name, Second_Name, Third_Name) values ('0001010061181',’BABA’,’N’,’’,’’,’NOWEZILE’,’’,’’)
insert into prodmgr.t_unverified (national_id, surname, First_Initial, Second_Initial,Third_Initial, First_Name, Second_Name, Third_Name) values (0001010077097,’COERTZEN’,’A’,’C’,’J’,’AZIL’,’CUBITT’,’JONO’)

Parse simple XML in SQL Server

How to parse the following xml as recordset?
<root>
<240>0</240>
<241>1</241>
<242>2</242>
<243>3</243>
<249>4</249>
</root>
<root 240="0" 241="1" 242="2" 243="3" 249="4"/>
When I try
declare #ids xml = N'<root><240>0</240><241>1</241></root>'
SELECT T.Item.value('240[1]', 'int')
from #ids.nodes('/root') AS T(Item)
I get an error
ML parsing: line 1, character 8, illegal qualified name character: declare #ids xml = N'<240>0' SELECT T.Item.value('a[1]', 'int') from #ids.nodes('/root') AS T(Item)
But generally I need the following output:
|240|0|
|241|1|
...
When xml elements are named as usual everything is Ok (<row key=240 value="0"/>).
XML disallows to use numbers as first character of an element name. Use format from your example:
<row key=240 value="0"/>

Resources