How to change from regexp_extract to regexp_substr in Snowflake - hive

I have some expressions in Hive that I need to change into Snowflake
(regexp_extract(subtransactionxml,'(.*?)()',1) in('REFUND'))
I tried to use this one but it gives me 0 results
(regexp_substr(subtransactionxml,'\W+(\W+)',1,1,'e',1) in('REFUND'))
Where is my mistake?

Based on your sample, the following query should work:
with xmldata as (
select '<transactionDate>2019-07-26T14:06:05.575-04:00</transactionDate> <type>CANCEL</type>' subtransactionxml )
select regexp_substr(subtransactionxml,'<type>(.*)<\/type>',1,1,'e',1) in ('CANCEL')
from xmldata;

Related

Big query unnest array with json values

Lets consider the following table on Google BigQuery:
WITH example AS (
SELECT 1 AS id, ["{\"id\":1, \"name\":\"AAA\"}", "{\"id\":2, \"name\":\"BBB\"}","{\"id\":3, \"name\":\"CCC\"}"]
UNION ALL
SELECT 2 AS id, ["{\"id\":5, \"name\":\"XXX\"}", "{\"id\":6, \"name\":\"ZZZ\"}"]
)
SELECT *
FROM example;
I would like to compose a query that will return names with their parent row's id.
like:
I tried using unnest with json functions and I just cant make this right.
Can anyone help me?
Thanks
Ido
According to your query, you already have json elements in your array. So with the use of unnest, you can use a json function like json_value to extract the name attribute of your elements.
select
id,
json_value(elt, '$.name')
from example, unnest(r) as elt;

What is similar function in snowflake for REGEXP_EXTRACT function in Bigquery?

Bigquery :
SELECT *,
REGEXP_EXTRACT(AM_EMAIL, '^[a-zA-Z0-9_.+-]+') as x_ALIAS,
"https:" AS COL1
FROM `agm-data-.global_i.P_ROUTING`;
Want to Convert this bigquery into Snowflake ,Please suggest on REGEXP_EXTRACT Bigquery similar function in snowflake?
In Snowflake you would use REGEXP_SUBSTR(). The regex you are using should work just fine, so:
REGEXP_EXTRACT(AM_EMAIL, '^[a-zA-Z0-9_.+-]+') as x_ALIAS
Note that [a-zA-Z0-9_] can also be expressed as [:alnum:], so:
REGEXP_EXTRACT(AM_EMAIL, '^[[:alnum:].+-]+') as x_ALIAS

how to fetch range of values using LIKE operator in oracle?

I my DB i want to select data which having A15-A19 using LIKE operator but couldnt get required result.
the code i made as SELECT * FROM MASTER_RULE WHERE VALUE BETWEEN LIKE 'A15%' AND 'A19%' and also tried regular expression as SELECT * FROM MASTER_RULE WHERE REGEXP_LIKE(value, 'A[1-9]') . But regexp gives all records not specified range 15-19.
How to achieve the solution for this?
Your first query is not ok, it has one extra keyword that you do not need.
Here is the regexp_like solution:
SELECT * FROM MASTER_RULE WHERE REGEXP_LIKE(value, '^A[1][5-9]')
Here is a demo
UPDATE:
Here is the "BETWEEN SOLUTION":
SELECT *
FROM MASTER_RULE
WHERE substr(value, 2,length(value)-1) between 15 AND 19
You could just use regular string comparisons:
where value >= 'A15' and
value < 'A20'
Not only is this simple, but the code can also take advantage of an index on value.
As you mentioned in the comments your data is like A15, A16, A17. etc you can achive your requirement with simple in clause also.
SELECT * FROM MASTER_RULE WHERE VALUE in ('A15','A16','A17','A18,'A19');

how to find the count of substring in string using BigQuery?

I want to find how many times "fizz" appears in "fizzbuzzfizz" string in bigquery or sql.
here output should be 2.
You can use REGEXP_EXTRACT_ALL and ARRAY_LENGTH, See this sql:
WITH data AS(
SELECT 'fizzbuzzfizz' as string
)
SELECT
ARRAY_LENGTH(REGEXP_EXTRACT_ALL(string, "fiz")) AS size FROM data;
Which produces this:

How to convert this SAS code to SQL Server code?

SAS CODE:
data table1;
set table2;
_sep1 = findc(policynum,'/&,');
_count1 = countc(policynum,'/&,');
_sep2 = findc(policynum,'-');
_count2 = countc(policynum,'-');
_sep3 = findc(policynum,'_*');
_count3 = countc(policynum,'_*');
How can I convert this into a select statement like below:
select
*,
/*Code converted to SQL from above*/
from table2
For example I tried the below code:
select
*,
charindex('/&,',policynum) as _sep1,
LEN(policynum) - LEN(REPLACE(policynum,'/&,','')) as _count1
from table2
But I got a ERROR 42S02: Function 'CHARINDEX(UNKNOWN, VARCHAR)' does not exist. Unable to identify a function that satisfies that given argument types. You may need to add explicit typecasts.
Please note that the variable pol_no is: 'character varying(50) not null'.
I am running this on using Aginity Workbench for Netezza. I believe this is IBM.
Assuming Oracle based on CHARINDEX() this may work:
You need to apply it twice, once for each character and take the minimum to find the first occurrence.
There may be a better suited function within Oracle, but I don't know enough to suggest one.
select
*,
min(charindex('/',policynum), charindex('&', policynum)) as _sep1
from table2
EDIT: based on OP notes.
Netezza seems like IBM which means use the INSTR function, not CHARINDEX.
select
*,
min(instr(policynum, '/'), instr(policynum, '&')) as _sep1
from table2
https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.sqls.doc/ids_sqs_2336.htm
FINDC & COUNTC functions are basically used for searching a character & counting them.
You can use LIKE operator from SQL to find characters with '%' and '_' wildcards
e.g. -
SELECT * FROM <table_name> WHERE <column_name> LIKE '%-%';
and
SELECT COUNT(*) FROM <table_name> WHERE <column_name> LIKE '%-%';
You can use regular expressions in the LIKE operator as well