Find specific strings All Position from large string - sql

I want to find all position of 'LIID' string into 'DATASTRING' column using which help to find all LLID values.
DATASTRING Column contains MIXED data its not valid XML file.
Query :
select id , DATASTRING from mixdatatable
Output :
id DATASTRING
1 ABC XYZ IS 1 ..
<ObjectValue /><CP /><SIProps ST="4" PL="False" PLS="" />
<STI_SummaryActiveProblemsField LIID="cdbd7044-ccde-11db-8cba-df0a56d89593" IID="37742a5f-7998-4715-8d43-0d7a19284d44" IR="True" RW="1">
<HD Title="Active Problems" /><ObjectValue />
<CP><PosReplace /><NegReplace /></CP><SIProps ST="4" PL="False" PLS="" />
-<STI_DxItem IID="d85c91e7-703f-4f8c-9a38-961fb2d14828" RW="1" LIID="03e4da86-d57c-408e-9479-b6fb97cc9a60"><HD Title=" unspecified "/>
<STI_US><ObjectValue><TextValue><![CDATA[
]]></TextValue></ObjectValue><CP />
<LIID='71194038-8ffb-488b-8af5-5f1f1a679115'>
<SIProps ST="1" SS=" " PL="False" PLS="" />
</STI_US> AAAAAA
979816816/881783683618317381-817
I want to find all LIID values. LLID values lenth are '36' eg.'cdbd7044-ccde-11db-8cba-df0a56d89593'
Expected Output :
ID LLID
1 cdbd7044-ccde-11db-8cba-df0a56d89593
1 03e4da86-d57c-408e-9479-b6fb97cc9a60
1 71194038-8ffb-488b-8af5-5f1f1a679115
PostgreSQL version 8.3
Note : i dont have acess to create function or procedure.

select id, unnest(regexp_matches(datastring,'LIID=[''"]([0-9a-f-]+)','g'))
from mixdatatable
Test on sqlfiddle.com

Related

extract xml tag based on max xpath of list value

I'm attempting to extract a currency value from an xml list, which is at the same level as the maximum value of another tag (within the same list).
I am using Oracle SQL 11gR2.
The maximum value extract is based on the xpath max() function.
I've then attempted to subscript the list (for currency tag) based on the result of the max(), but the currency appears as NULL.
This is a small sample of the data, and the associated xpath's used:
with xml_data as (
select xmltype('<?xml version="1.0" encoding="ISO-8859-1"?>
<SOME_OBJECT xmlns="http://www.example.com/xxyy/">
<ILA>
<PRODUCT_LIST>
<PRODUCT>
<MAP_ENTRY>
<CURRENCY_ENTRY>EUR</CURRENCY_ENTRY>
</MAP_ENTRY>
<INITIAL_VALUE>1.4219777502E8</INITIAL_VALUE>
</PRODUCT>
<PRODUCT>
<MAP_ENTRY>
<CURRENCY_ENTRY>ZAR</CURRENCY_ENTRY>
</MAP_ENTRY>
<INITIAL_VALUE>1.4612991655E8</INITIAL_VALUE>
</PRODUCT>
<PRODUCT>
<MAP_ENTRY>
<CURRENCY_ENTRY>USD</CURRENCY_ENTRY>
</MAP_ENTRY>
<INITIAL_VALUE>1.4712991655E8</INITIAL_VALUE>
</PRODUCT>
</PRODUCT_LIST>
</ILA>
</SOME_OBJECT>') as msg from dual union all
select xmltype('<?xml version="1.0" encoding="ISO-8859-1"?>
<SOME_OBJECT xmlns="http://www.example.com/xxyy/">
<ILA>
<SUBSEQUENT_VALUE>10266</SUBSEQUENT_VALUE>
</ILA>
</SOME_OBJECT>') as msg from dual
)
--
select x.subsequent_value, x.max_initial_value ,x.currency
from xml_data d
,xmltable (xmlnamespaces(default 'http://www.example.com/xxyy/')
,'/SOME_OBJECT' passing d.msg
columns
max_initial_value number path 'max(ILA/PRODUCT_LIST/PRODUCT/INITIAL_VALUE)'
,currency varchar2(3) path 'ILA/PRODUCT_LIST/PRODUCT[INITIAL_VALUE=max(ILA/PRODUCT_LIST/PRODUCT/INITIAL_VALUE)]/MAP_ENTRY/CURRENCY_ENTRY'
,subsequent_value number path 'ILA/SUBSEQUENT_VALUE'
) as x;
So the existing output is:
SQL> #get_max
SUBSEQUENT_VALUE MAX_INITIAL_VALUE CUR
---------------- ----------------- ---
147129917
10266
The first line should include USD.
Any suggestions as to what the xpath should be ?
Your xpath looks fine to me in theory,
im not an expert but there might be a problem here :
your displayed value of MAX_INITIAL_VALUE is 147129917 so it has been rounded right ?
Therefore, it cannot be equal to the actual value of 1.4712991655E8 in your XML object
maybe if there is a round function :
currency varchar2(3) path 'ILA/PRODUCT_LIST/PRODUCT[INITIAL_VALUE=round(max(ILA/PRODUCT_LIST/PRODUCT/INITIAL_VALUE))]/MAP_ENTRY/CURRENCY_ENTRY'
it would probably take a second argument for which decimal to round at if it exists
The full path has to be specified for the max() function, as follows:
ILA/TION/PAWS/PRODUCT_LIST/PRODUCT[INITIAL_VALUE = max(/SOME_OBJECT/ILA/TION/PAWS/PRODUCT_LIST/PRODUCT/INITIAL_VALUE)]/MAP_ENTRY/CURRENCY_ENTRY'
Using the same data from the original post, with a modified SQL statement, that includes the full path shows up the currency:
select x.subsequent_value, x.max_initial_value ,x.currency
from xml_data d
,xmltable (xmlnamespaces(default 'http://www.example.com/xxyy/')
,'/SOME_OBJECT' passing d.msg
columns
p XMLTYPE PATH '.'
, max_initial_value number path 'max(ILA/TION/PAWS/PRODUCT_LIST/PRODUCT/INITIAL_VALUE)'
, currency varchar2(3) path 'ILA/TION/PAWS/PRODUCT_LIST/PRODUCT[INITIAL_VALUE = max(/SOME_OBJECT/ILA/TION/PAWS/PRODUCT_LIST/PRODUCT/INITIAL_VALUE)]/MAP_ENTRY/CURRENCY_ENTRY'
, subsequent_value number path 'ILA/TION/PAWS/SUBSEQUENT_VALUE'
) as x;
SUBSEQUENT_VALUE MAX_INITIAL_VALUE CURRENCY
---------------- ----------------- --------
147129917 USD
10266
Alternatively, playing around with virtual paths might also do the trick, but it will slow down your query.

SELECT only specific value from XML column in SQL

Running the following TSQL
SELECT Name FROM Category
returns these results as the column in SQL is of xml data type.
1 <locale en-US="Abstract" />
2 <locale en-US="African" />
3 <locale en-US="Americana" />
I'd like to get a result set like:
1 Abstract
2 African
3 Americana
How can I do this?
You can try to use the .value function
SELECT Name.value('(/locale/#en-US)[1]','varchar(20)')
from Category
sqffiddle

in the below data how can i write query ,for counting number of employees having skill html?

var myMenu = $( `
<menu type="context" id="table">
</menu>
` ).appendTo ("body");
id name skill
1 abc html,java,css
2 xyz html,c,other
you can use the following query.
select count(*) from yourtable where find_in_set("html",skill) > 0;
find_in_set from Hive documentation
Returns the first occurance of str in strList where strList is a
comma-delimited string. Returns null if either argument is null.
Returns 0 if the first argument contains any commas. For example,
find_in_set('ab', 'abc,b,ab,c,def') returns 3.

XML Data into Row Selection

I have a single field in a DB which contains columns of data, but in an continuous string of XML.
I want to extract the data from the XML, and return as normal columns.
There could be multiple rows of data within this single field.
Here are 2 real examples of data in this field
Example 1:
<DocumentElement>
<AuditTrailDetail>
<TableName>order_header_total</TableName>
<RowID>837</RowID>
<AuditType>Edit</AuditType>
<FieldName>oht_foreign_net</FieldName>
<ValueFrom>65.1600</ValueFrom>
<ValueTo>115.1600</ValueTo>
<EntityName />
<DisplayName />
<Identifier />
</AuditTrailDetail>
<AuditTrailDetail>
<TableName>order_line_item</TableName>
<RowID>2442</RowID>
<AuditType>Edit</AuditType>
<FieldName>oli_qty_required</FieldName>
<ValueFrom>1.0000</ValueFrom>
<ValueTo>2.0000</ValueTo>
<EntityName />
<DisplayName />
<Identifier>61 - test</Identifier>
</AuditTrailDetail>
</DocumentElement>
Example 2:
<DocumentElement>
<AuditTrailDetail>
<TableName>order_line_item</TableName>
<RowID>2446</RowID>
<AuditType>Edit</AuditType>
<FieldName>oli_description</FieldName>
<ValueFrom>2 Ply Tissue Masks</ValueFrom>
<ValueTo>2 Ply Tissue Masksdd</ValueTo>
<EntityName />
<DisplayName />
<Identifier>D/D170 - 2 Ply Tissue Masksdd</Identifier>
</AuditTrailDetail>
</DocumentElement>
For Example 1, I'd like to return 2 rows as below:
For Example 2 there is one row, and I'd like it as below:
The fields in the table are:
owat_id
owat_audit_type
owat_record_id
owat_record_type
owat_datetime
owat_ud_id
owat_details
The XML data is within owat_details.
Assume the table name is Audit_Trail.
How can I do this directly in the SQL query?
Thank you to Johnie Karr
I have got this working using XQuery.
I have to do 2 queries and union them together to get the 2 separate elements in one field as separate rows. I'm sure there is a cleaner solution, but this works.
Thank you very much :)
SELECT
AuditTrail.owat_record_type,
AuditTrail.owat_audit_type,
AuditTrail.owat_datetime as 'Audit Date',
user_detail.ud_username as 'User',
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[1]','varchar(50)') AS TableName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/RowID)[1]','varchar(50)') AS RowID,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/FieldName)[1]','varchar(50)') AS FieldName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueFrom)[1]','varchar(50)') AS ValueFrom,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueTo)[1]','varchar(50)') AS ValueTo,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/Identifier)[1]','varchar(50)') AS Identifier
FROM
(select
orderwise_audit_trail.owat_id,
orderwise_audit_trail.owat_audit_type,
orderwise_audit_trail.owat_record_id,
orderwise_audit_trail.owat_record_type,
orderwise_audit_trail.owat_datetime,
orderwise_audit_trail.owat_ud_id,
convert(xml, orderwise_audit_trail.owat_details) as owat_details
from
orderwise_audit_trail) as AuditTrail
left join user_detail on user_detail.ud_id = AuditTrail.owat_ud_id
Where AuditTrail.owat_details is not NULL
and (AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[1]','varchar(50)')) is not null
UNION ALL
SELECT
AuditTrail.owat_record_type,
AuditTrail.owat_audit_type,
AuditTrail.owat_datetime as 'Audit Date',
user_detail.ud_username as 'User',
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[2]','varchar(50)') AS TableName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/RowID)[2]','varchar(50)') AS RowID,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/FieldName)[2]','varchar(50)') AS FieldName,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueFrom)[2]','varchar(50)') AS ValueFrom,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/ValueTo)[2]','varchar(50)') AS ValueTo,
AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/Identifier)[2]','varchar(50)') AS Identifier
FROM
(select
orderwise_audit_trail.owat_id,
orderwise_audit_trail.owat_audit_type,
orderwise_audit_trail.owat_record_id,
orderwise_audit_trail.owat_record_type,
orderwise_audit_trail.owat_datetime,
orderwise_audit_trail.owat_ud_id,
convert(xml, orderwise_audit_trail.owat_details) as owat_details
from
orderwise_audit_trail) as AuditTrail
left join user_detail on user_detail.ud_id = AuditTrail.owat_ud_id
Where AuditTrail.owat_details is not NULL
and (AuditTrail.owat_details.value('(/DocumentElement/AuditTrailDetail/TableName)[2]','varchar(50)')) is not null
order by 1, 2, 3, 4

How to use Xquery in Sql Server to search for character in column that stores XML data

There is column named FileImage that contains XML data.
<root>
<Image>
<File_image>02Jan12- Mfg--7.jpg</File_image>
<Page_no>7</Page_no>
<Logo>N</Logo>
</Image>
</root>
I want to search whether text between tags contains space character.
This is for checking invalid file name.
SELECT *
FROM mytable
where fileimage.exist('
/Image:file_image//text()
[contains(., " ")]') = 1
But error shown
XQuery [mytable.FileImage.exist()]: The name "Image" does not denote a namespace.
How to do this ? I am using SQL Server 2005.
SELECT *
FROM mytable
where fileimage.exist('
/root/Image/File_image//text()
[contains(., " ")]') = 1