Extracting date from XML field in SparkSQL - xpath

I have a SQL table with a field called xml where a XML structure is stored as a string.
I would like to extract this as a string/date somehow. There are multiple days/months/years and I only want to extract the first one. To extract just the day the following works fine:
SELECT explode(xpath(xml, '(//Day/text())[1]')) FROM table_name
I could do this for month and year and then concate everything. But how can I do it with one select query? I tried using concat:
SELECT explode(xpath(xml, 'concat((//Day/text())[1], (//Month/text())[1]')) FROM table_name
But then I get the following error:
Can not convert #STRING to a NodeList

Related

How to get the phone num leading with zero while exporting the data to excel ,csv,HTML

i am using the below query
select ''||'001234567890'||'' from dual;
this will work for .xslx and HTML but csv its not working
is there any another way to show where for all three formats it will show correct.
In Oracle, an empty string '' is the same as NULL so your query:
select ''||'001234567890'||'' from dual;
Is the same as:
select NULL||'001234567890'||NULL from dual;
Which is the same as:
select '001234567890' from dual;
According to this answer, for a csv file that you are importing into Excel, you want the format "=""001234567890""" which would give the query:
select '"=""001234567890"""' from dual;
or, if you are concatenating strings:
select '"=""' || '001234567890' || '"""' from dual;
There will not be one single method that works for all 3 formats as, although HTML and Excel's .xlsx format are both XML derivatives (yes, HTML is not XML) and you can probably find something that works for both of those, CSV is a very different format.

Entity-framework generated query throws ORA-12704 when using TO_CHAR or TO_NUMBER

I currently face the problem, that I get an exception when executing a query generated by the entity framework.
The query worked until I've joined another table using .include() to the existing entity. Now, whenever I execute the query, I get an Oracle error ORA-12704 character set mismatch.
I narrowed the problem down to the following:
Before joining the table, the generated SQL is a simple query with some join statements. After joining another table, the generated SQL cointains two subqueries which are combined using UNION ALL. In one of the subqueries, a lot of helper-columns are selected.
They look like this:
SELECT
... some other columns...
TO_NUMBER(NULL) AS C1,
TO_CHAR(NULL) AS C2,
...
If I remove those columns and also the corresponding ones in the other subquery, no error is thrown. When I replace the columns with NULL instead of TO_XXX(NULL), the query also works as expected.
Is there any way force the entity-framework not to use these problematic casts?
The problem is caused with the usage of a NVARCHAR2 column in combination with the function TO_CHAR (that returns VARCHAR2 data type) as illustrated below
create table tab
(txt nvarchar2(10));
select txt from tab
union all
select to_char(null) from dual;
ORA-12704: character set mismatch
So your goal is to motivate the tool to generate a query that uses either TO_NCHAR(null) or cast(null as nvarchar2(10)) - both will work.
To do this, you need to add the following data-annotation to the property of the corresponding entity:
[Column(TypeName = "NVARCHAR2")]
The given TypeName must match with the type of the column in the database.
After this addition, the entityframework will generate the correct casts. In this case, the following cast ist generated:
SELECT
...
TO_NCHAR(NULL),
...
You should see no problem with the to_number(null) if the corresponding UNIONcolumn is of a number datatype.

Oracle query to read nested JSON data

I have a table which has a column with nested JSON data that I want to read for a query result. But the data type of column is VARCHAR and data inside is a JSON string with nested objects inside.
Now when I hit below query it works fine and gives me result,
select * from dataTable where regexp_like(metadata,'(*)"id":"33001"(*)');
Below is the metadata column of dataTable :
{"id":"33001",
"digits":"1234",
"requestId":"5d54-f6-48-8d-8155190",
"deliveryMethod":"ATT",
"messageStatus":"{\"status\":[
{\"tokenId\":\"Zktx\",\"deliveryStatus\":\"SUCCESS\",\"code\":\"0\"},
{\"tokenId\":\"aGsx\",\"deliveryStatus\":\"SUCCESS\",\"code\":\"0\"}
]}"
}
Above data is all String in single metadata column, I have split it just so that it is more readable.
But I also want to filter the data based on "deliveryStatus" contents. So when I try below query,
select * from dataTable where regexp_like(metadata,'(*)"id":"33001"(*)') AND regexp_like(metadata,'(*)\"deliveryStatus\":\"SUCCESS\"(*)');
It doesn't work. Does NOT show any result. There is no error though. I feel I need some other approach to read the nested JSON contents inside that string. But I am not sure how to do that.
Can someone provide any insights of how to achieve this?
Backslash is an escape character in regex, so you have to escape it with a second backslash.
-- sample data
with datatable as (select '{"id":"33001",
"digits":"1234",
"requestId":"5d54-f6-48-8d-8155190",
"deliveryMethod":"ATT",
"messageStatus":"{\"status\":[
{\"tokenId\":\"Zktx\",\"deliveryStatus\":\"SUCCESS\",\"code\":\"0\"},
{\"tokenId\":\"aGsx\",\"deliveryStatus\":\"SUCCESS\",\"code\":\"0\"}
]}"
}' metadata from dual)
-- actual query
select * from dataTable where regexp_like(metadata,'(*)"id":"33001"(*)')
AND regexp_like(metadata,'(*)\\"deliveryStatus\\":\\"SUCCESS\\"(*)'); -- note double backslashes
But I do recommend looking into the native JSON support if you're on Oracle 12c or up, like #thatjeffsmith mentioned. Regex work, but they're expensive and fragile.

wrong date format in bi reports oracle

I have the following date which is in varchar2(11) column in database:
select valid_untill from SALES_ORDERS_V where header_id = 7999410;
30-May-2016
Using rtf template and xml source, the report output (PDF) is:
4950-11-19 04:45:49:0
I don't know its equal to "30-May-2016".
Why this is showing this, as I did not do any formating in rtf?
Not familiar with either RTF or XML-Publisher, but whenever you retrieve a date saved in string format, IF you use it as a date in your code and not as a string, you must make sure you retrieve it correctly.
In this case, with your select statement: it shouldn't be select valid-until from... (or is it really misspelled, with two l at the end: valid_until?) If it is meant to be used as a date, it should be
select to_date(valid_until, 'dd-Mon-yyyy') from ...
Really the problem here is that the date is stored as a string and not in the date datatype. Good luck!

timestamp not working in hive

I have a table with one column data type as 'timestamp'. Whenever i try to do some queries on the table, even simple select statement i am getting errors.
example of a row in my column,
'2014-01-01 05:05:20.664592-08'
The statement i am trying,
'select * from mytable limit 10;'
the error i am getting is
'Failed with exception java.io.IOException:java.lang.NumberFormatException: For input string: "051-08000"'
Date functions in hive like TO_DATE are also not working.If i change the data type to string, i am able to extract the date part using substring. But i need to work with timestamp.
Has anyone faced this error before? Please let me know.
Hadoop is having trouble understanding '2014-01-01 05:05:20.664592-08' as a timestamp because of the "592-08" at the end. You should change the datatype to string, cut off the offending portion with a string function, then cast back to timestamp:
select cast(substring(time_stamp_field,1,23) as timestamp) from mytable

Resources