Regular Expressions Oracle - oracle

I want to find regular expression [.-] in field filial_name.
select uc.filial_name from MYTABLE uc
where regexp_like(uc.filial_name , '[.-]');
select uc.filial_name from MYTABLE uc
where uc.filial_name like '%[.-]%';
The first variant is working. But the second is not.
How to fix second variant ?

Second expression isn't regex , it is normal search and should be used be like '%text%' .
Try this
SELECT uc.filial_name FROM MYTABLE uc
where uc.filial_name like '%.-%';

Related

How to use subquery in LIKE statement in Impala?

I have a lookup table that contains forbidden values/strings and a rule number which depicts where the value cannot occur. So for e.g., I have ‘C/O’ as a value and this can’t occur anywhere in a name field. I also have ‘P.O’ which can’t occur in an address. I am attempting to create a data quality report to flag these values without hard coding. I have tried:
Select
A.name
,A.address
From customer A
Where a.name LIKE (Select concat(‘%’, exclusion_value, ‘%’) from DQ_lookup where rule_number=2)
Or a.address LIKE (Select concat(‘%’, exclusion_value, ‘%’) from DQ_lookup where rule_number=1)
This fails. How if at all can I get this to work ?
For matching patterns in hive, you would need to use rlike.
A RLIKE B
: NULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, 'foobar' RLIKE 'foo' evaluates to TRUE and so does 'foobar' RLIKE '^f.*r$'.
Something as below would do.
Select
A.name,
A.address
From customer A
Where
a.name RLIKE (Select exclusion_value from DQ_lookup where rule_number=2)
OR a.address RLIKE (Select exclusion_value from DQ_lookup where rule_number=1)
Note: exclusion_value should be a regex expression.

can I use `=` sign operator with sub-query instead of `IN`

I am just wondering if use = sign operator with sub-query instead of IN
Is it correct way ? and meet the oracle standard ?
Example
select column_name from my_table_1 where id = (select max(id) from my_table_2);
The Difference is related to the number of rows returned. If you have only one row returned from nested sql you may prefer both = or in operators. But if multiple rows returned from nested query, use in operator.
So, in your sql example you may prefer using any of the operators. Since, max functions returns only one row.
As you are fetching maximum value from subquery to compare with id, Both(= and IN )will work fine. But If you are trying to fetch more than one row then you have to use IN keyword.
If you have 1 result in sub query you are fine with using = sign, except when data type is wrong, for example , checking with same data type of dummy VARCHAR2(1)
select * from dual where 'X' = (select max(dual.dummy) from dual);
Is similar to using in (also same explain plain)
select * from dual where 'X' in (select max(dual.dummy) from dual);
But checking with different/wrong data type will result with exception ORA-01722 Invalid number
select * from dual where 1 =(select max(dual.dummy) from dual);

How to get result from two queries in same output window?

I need output from the below two queries simultaneously in one output window.
QUERY 1
SELECT C.SERVICENAME, C.SERVICEID , B.SOAPIN, B.SOAPOUT, A.TIMESTAMP
FROM Schema1.LG_LOGENTRIES A, Schema1.LG_SOAPREQUESTS B, Schema1.CFG_SOAPSERVICES C
WHERE B.SERVICEID =C.SERVICEID AND
C.SERVICENAME <>'UploadAndPrepareDocumentEx1__sdweb_services_preload' AND
A.ID=B.LOGENTRYID AND B.TIMESTAMP BETWEEN TO_DATE('02/01/2018 11:55:00','dd/mm/yyyy hh24:mi:ss')
AND TO_DATE('02/01/2018 12:03:59','dd/mm/yyyy hh24:mi:ss') AND A.USERID IN (SELECT ID FROM Schema1.CFG_USERS
WHERE NAME=UPPER(TO_CHAR('CGBXGVSG')));
Query 2
SELECT B.JSONIN, B.JSONOUT, A.TIMESTAMP, B.EVENT_MESSAGE, A.PROCESSID, A.status, A.SERVERNAME
FROM Schema1.LG_LOGENTRIES A, Schema1.LG_EVENT B
WHERE B.EVENT_MESSAGE NOT IN ('getFileImage','submitBase64','loadDocumentToSign','getRefData') AND
A.ID=B.LOG_ENTRYID AND B.TIMESTAMP BETWEEN TO_DATE('31/12/2017 13:43:00','dd/mm/yyyy hh24:mi:ss')
AND TO_DATE('31/12/2017 13:53:59','dd/mm/yyyy hh24:mi:ss') AND A.USERID IN (SELECT ID FROM Schema1.CFG_USERS
WHERE NAME=UPPER(TO_CHAR('CTHX8Y2G')));
Run with F5 - you'll get both queries' output in the script panel.
I talk about how this differs here
UNION might be one option, but you'll have to
uniform both column lists (i.e. they have to return the same number of columns which have to be of the same data type), which means that you'd have to add certain NULL columns to both queries
include additional identifier so that you'd know which SELECT returned which values
If you wanted to have them side-by-side, huh, that's not that easy. Thinking loudly: you'd have to have a column that joins those values. Those SELECTs would be inline views. You'd use an aggregate function (such as MAX) along with a DECODE (or CASE) to select values from both queries. Shortly: too much pain.
Now, why do you want to do that? What's wrong with two separate windows, placed side by side?
[EDIT] Showing example of how UNION might look like
select c.servicename, c.serviceid, b.soapin, b.soapout, a.timestamp, to_char(null), to_char(null), to_char(null) , to_number(null), to_char(null), to_char(null)
from ... the rest of your 1st query
union
select null , null , null , null , a.timestamp, b.jsonin , b.jsonout , b.event_message, a.processid , a.status , a.servername
from ... the rest of your 2nd query

ORA-00979 Not a GROUP BY Expression with complex EXTRACTVALUE

I'm trying to execute a rather simple Oracle query against a table with a XMLTYPE column:
Select POB_CODPOL, CODSIS From (
Select T1.POB_CODPOL, EXTRACTVALUE(T1.POB_XMLPOL, '/Polbas/polfun[nomfun="filterBySystem"]/extpar[codele="codsis"]/valele/text()') CODSIS
From TDTX_POLITICA_CLOB T1
Where T1.POB_CODEMP = '001840'
)
Group By POB_CODPOL, CODSIS
This throws a ORA-00979 Not a GROUP BY Expression, which I don't really understand.
Even worse: when I execute the exact same query, but with a simplified XPATH query does work:
Select POB_CODPOL, CODSIS From (
Select T1.POB_CODPOL, EXTRACTVALUE(T1.POB_XMLPOL, 'Polbas/codpol/text()') CODSIS
From TDTX_POLITICA_CLOB T1
Where T1.POB_CODEMP = '001840'
)
Group By POB_CODPOL, CODSIS
It looks like Oracle doesn't like conditions like [nomfun="filterBySystem"] when using a GROUP BY (without the grouping clause, everything works fine).
Any idea on why this can be happening?
Edit: the result of the inner query is rather simple:
EXTRACTVALUE is deprecated.
Oracle recommends to use XMLQUERY, XMLTABLE for it.
This one should work:
WITH t as
(SELECT T1.POB_CODPOL, x.CODSIS
FROM TDTX_POLITICA_CLOB T1
NATURAL JOIN XMLTABLE('/Polbas/polfun[nomfun="filterBySystem"]/extpar[codele="codsis"]/valele‌​'
PASSING POB_XMLPOL COLUMNS
CODSIS VARCHAR2(50) PATH '/') x
Where T1.POB_CODEMP = '001840')
SELECT POB_CODPOL, CODSIS
FROM t
GROUP BY POB_CODPOL, CODSIS;
There's a Bug 28588011 : "ORA-00979: NOT A GROUP BY EXPRESSION" WHEN TABLE FORMAT IS BINARY XML.
Severity 2 - Severe Loss of Service
Created 02-Sep-2018
Status 11 - Code/Hardware Bug (Response/Resolution)
WORKAROUND No
I guess they won't fix it. But the hint NO_XML_QUERY_REWRITE (that I use for other XML related bugs, too) worked for me.

Is there a Hive equivalent of SQL “LIKE ANY ( SUBQUERY )”

While Hive doesn't supports multi-value LIKE queries which are supported in SQL : ex.
SELECT * FROM user_table WHERE first_name LIKE ANY ( 'root~%' , 'user~%' );
We can convert it into equivalent HIVE queries as :
SELECT * FROM user_table WHERE first_name LIKE 'root~%' OR first_name LIKE 'user~%'
Does anyone know an equivalent solution that Hive does support in case sub-query is used with LIKE ? Have a look at below example :
SELECT * FROM user_table WHERE first_name LIKE ANY ( SELECT expr FROM exprTable);
As It doesn't have values in expression, I can't use same approach for generating multiple LIKE expression separated with OR / AND operator. Initially I thought to write HIVE UDF for it ? Can you please help me supporting such expression and finding HIVE equivalent ?
You can use Hive's RLIKE relational operator as shown below,
SELECT * FROM user_table WHERE first_name RLIKE 'root~|user~|admin~';
Hope this helps!
This is a case involving theta joins in Hive. There is a wiki page for this and a jira request. Please go through the details here on this page: https://cwiki.apache.org/confluence/display/Hive/Theta+Join
Your case is similar to the Side-Table Similarity case given on the page.
You need to convert the expr values into a map and then use regular expression to find the like. Alternatively you can also use union all with all the like expressions in separate SQL - the query might become tedious so you can programatically generate it.
What about this using EXISTS
SELECT * FROM user_table WHERE EXISTS ( SELECT * FROM exprTable WHERE first_name LIKE expr );

Resources