Fetch Substring in Oracle - sql

I have a string as -
V_TAG_B = utm_source=google_search&utm_medium=cpc&utm_term={Keyword}&utm_campaign=home-|-SBI-|-search
I need to break this string into 4 small parts as -
V_UTM_SOURCE = utm_source=google_search&
V_UTM_MEDIUM = utm_medium=cpc&
V_UTM_TERM = utm_term={Keyword}&
V_UTM_CAMPAIGN = utm_campaign=home-|-SBI-|-search
I need to do this because the string can be in any order such as utm_campaign coming first and utm_source is coming at last. So after breaking it into pieces i will concat it again and will match with our DB table in which a column have the same value as this string. I have achieved this using SUBSTR/INSTR combination as below -
-- Assigning First Keyword
IF UPPER(SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '=', 1, 1)-1)) LIKE '%UTM_SOURCE%' THEN
V_UTM_SOURCE := SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '&', 1, 1));
ELSIF UPPER(SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '=', 1, 1)-1)) LIKE '%UTM_MEDIUM%' THEN
V_UTM_MEDIUM := SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '&', 1, 1));
ELSIF UPPER(SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '=', 1, 1)-1)) LIKE '%UTM_TERM%' THEN
V_UTM_TERM := SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '&', 1, 1));
ELSE
V_UTM_CAMPAIGN := SUBSTR(V_TAG_B, 1, INSTR(V_TAG_B, '&', 1, 1));
END IF;
-- Assigning Second Keyword
IF UPPER(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1)) LIKE '%UTM_SOURCE%' THEN
V_UTM_SOURCE := SUBSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), 1, INSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), '&', 1, 1));
ELSIF UPPER(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1)) LIKE '%UTM_MEDIUM%' THEN
V_UTM_MEDIUM := SUBSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), 1, INSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), '&', 1, 1));
ELSIF UPPER(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1)) LIKE '%UTM_TERM%' THEN
V_UTM_TERM := SUBSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), 1, INSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), '&', 1, 1));
ELSE
V_UTM_CAMPAIGN := SUBSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), 1, INSTR(SUBSTR(V_TAG_B, INSTR(V_TAG_B, '&', 1, 1)+1), '&', 1, 1));
END IF;
I guess this can be shortly and easily achieved using REGEXP SUBSTR also. Any help/suggestion is appreciated.

You can use REGEXP_SUBSTR as following:
SQL> SELECT
2 REGEXP_SUBSTR(STR, 'utm_source=[^&]+') as V_UTM_SOURCE,
3 REGEXP_SUBSTR(STR, 'utm_medium=[^&]+') as V_UTM_MEDIUM,
4 REGEXP_SUBSTR(STR, 'utm_term=[^&]+') as V_UTM_TERM,
5 REGEXP_SUBSTR(STR, 'utm_campaign=[^&]+') as V_UTM_CAMPAIGN
6 FROM
7 ( SELECT 'V_TAG_B = utm_source=google_search&utm_medium=cpc&utm_term {Keyword}&utm_campaign=home-|-SBI-|-search' AS STR
8 FROM DUAL);
V_UTM_SOURCE V_UTM_MEDIUM V_UTM_TERM V_UTM_CAMPAIGN
------------------------- -------------------- -------------------- ---------------------------------
utm_source=google_search utm_medium=cpc utm_term={Keyword} utm_campaign=home-|-SBI-|-search
SQL>
Cheers!!

Related

How to SQL conver to dataframe

I want to convert to SQL to dataframe.\
SELECT day,
MAX(id),
MAX(if(device = 'Mobile devices with full browsers', 'mobile', 'pc')),
AVG(replace(replace(search_imprshare, '< 10%', '10'), '%', '') / 100),
REPLACE(SUBSTRING(SUBSTRING_INDEX(add_trackingcode, '_', 1), CHAR_LENGTH(SUBSTRING_INDEX(add_trackingcode, '_', 1 - 1)) + 2), add_trackingcode, '')
FROM MY_TEST_TABLE
GROUP BY day
But I can only do below that.
I don't know how to work on '???'.
df_data= df_data.groupby(['day').agg(
{
'id': np.max,
'device ' : ???,
'percent' : ???,
'tracking' : ???
}
)
How should I do it?

Tensorflow: Too many dimensions

I'm trying to create a TensorFlow (2.0) variable like this:
c_init = tf.zeros_initializer()
c = tf.Variable(initial_value=c_init(shape=shape, dtype="float32"), trainable=True)
the shape variable is this:
shape=(49, 52, 26, 49, 6, 3, 31, 11, 24, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
I'm getting this error message:
InvalidArgumentError: Too many dimensions [Op:Fill] name: zeros/
I did not know there is a limit in the number of dimensions. I did not see anything in TensorFlow documentation about it. Is there any way to get around this limitation?
The max number of dimensions is 254
Get around this limitation?
If you don't mind, I do have to ask you a question:
Are you sure you have that many dimensions in your problem? I have seen people mistakenly using size of dimension as number of dimensions. Are you sure you have so many dimensions with size 1?
Let's not forget that there is no need to represent anything as tensors. We could solve any problem without using a multi-dimensional data type (tensor). The reason why this type of representation is used is because it allows certain linear algebra operations to be applied and they are much fast when compared to regular loops in a more traditional code.
So, yes, you can get around this limitation but you will need to so some "soul search" and figure out what kind of math operations you are planning to apply to this humongous tensor.

Get values after and before specific character in SQL/PL SQL?

I have a string value as a parameter and I need to parse it. My value is :
param := ('1234#5432#4567#8763');
I have to get 1234, 5432, 4567 and 8763 values partially. I will set these values different parameters.
How can I solve it with SQL?
Thanks,
select level, regexp_substr(a,'\d+',1,level)
from(select '1234#5432#4567#8763' a from dual)
connect by level <= regexp_count(a,'#') + 1
Assuming that you are in PL/SQL and you need to split a value of a parameter or a variable into four variables, this could be a way:
declare
param varchar2(100);
param1 varchar2(100);
param2 varchar2(100);
param3 varchar2(100);
param4 varchar2(100);
begin
param := '1234#5432#4567#8763';
--
param1 := substr(param, 1, instr(param, '#', 1, 1)-1);
param2 := substr(param, instr(param, '#', 1, 1) +1 , instr(param, '#', 1, 2) - instr(param, '#', 1, 1)-1);
param3 := substr(param, instr(param, '#', 1, 2) +1 , instr(param, '#', 1, 3) - instr(param, '#', 1, 2)-1);
param4 := substr(param, instr(param, '#', 1, 3) +1 );
--
dbms_output.put_line('Param1: ' || param1);
dbms_output.put_line('Param2: ' || param2);
dbms_output.put_line('Param3: ' || param3);
dbms_output.put_line('Param4: ' || param4);
end;
With regular expressions, you can get the same result by searching the 1st, 2nd, ... occurrence of a string that is followed by a # or by the end of the line ('$'); a better explanation of this approach is described in the link gave by Gary_W in his comment
...
param1 := regexp_substr(param, '(.*?)(#|$)', 1, 1, '', 1 );
param2 := regexp_substr(param, '(.*?)(#|$)', 1, 2, '', 1 );
param3 := regexp_substr(param, '(.*?)(#|$)', 1, 3, '', 1 );
param4 := regexp_substr(param, '(.*?)(#|$)', 1, 4, '', 1 );
...

Missing Right parenthesis error with `=>`

I am getting the error after writing the below code for a simple insert..select stmnt . I am confused as there seems nothing wrong with syntax .
INSERT INTO dimension_tab(FACT_1_ID,FACT_2_ID,FACT_3_ID,FACT_4_ID,SALES_VALUE)
SELECT TRUNC(DBMS_RANDOM.value(low => 1, high => 3)) AS fact_1_id,
TRUNC(DBMS_RANDOM.value(low => 1, high => 6)) AS fact_2_id,
TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_3_id,
TRUNC(DBMS_RANDOM.value(low => 1, high => 11)) AS fact_4_id,
ROUND(DBMS_RANDOM.value(low => 1, high => 100), 2) AS sales_value
FROM dual
CONNECT BY level <= 1000;
COMMIT;
Syntax of DBMS_RANDOM.VALUE is
DBMS_RANDOM.VALUE(
low IN NUMBER,
high IN NUMBER
)
So you can use like this...
INSERT INTO dimension_tab (FACT_1_ID,FACT_2_ID,FACT_3_ID,FACT_4_ID,SALES_VALUE)
SELECT TRUNC(DBMS_RANDOM.value( 1, 3)) AS fact_1_id,
TRUNC(DBMS_RANDOM.value( 1, 6)) AS fact_2_id,
TRUNC(DBMS_RANDOM.value( 1, 11)) AS fact_3_id,
TRUNC(DBMS_RANDOM.value( 1, 11)) AS fact_4_id,
ROUND(DBMS_RANDOM.value( 1, 100), 2) AS sales_value
FROM dual
CONNECT BY LEVEL <= 1000;
COMMIT;
You are using a valid syntax, but as you are using oracle 10g it will not support =>, So you'll have to replace (low => 1, high => 3) with (1, 3).
Try like this,
INSERT INTO dimension_tab
SELECT TRUNC(DBMS_RANDOM.value( 1, 3)) AS fact_1_id,
TRUNC(DBMS_RANDOM.value( 1, 6)) AS fact_2_id,
TRUNC(DBMS_RANDOM.value( 1, 11)) AS fact_3_id,
TRUNC(DBMS_RANDOM.value( 1, 11)) AS fact_4_id,
ROUND(DBMS_RANDOM.value( 1, 100), 2) AS sales_value
FROM dual
CONNECT BY LEVEL <= 1000;

Oracle PLSQL - Error handling in UTL_FILE

My script as below, it will load a csv file to PRODUCT_TBL and it any error happened during the process, the script will rollback transaction and output an error message, however it does not print out the message when it hit UTL_FILE error, example invalid file operations. Any help are appreciated. Thanks
DECLARE
V_error_code NUMBER;
V_error_message VARCHAR2(255);
V_ignore_headerlines NUMBER := 1;
V_eof BOOLEAN := FALSE;
F UTL_FILE.FILE_TYPE;
V_LINE VARCHAR2 (32767);
V_PRD_ID PRODUCT_TBL.PRD_ID%TYPE;
V_PATTERN PRODUCT_TBL.PATTERN%TYPE;
V_REMARK PRODUCT_TBL.REMARK%TYPE;
V_CREATED_BY PRODUCT_TBL.CREATED_BY%TYPE;
V_CREATED_DATE PRODUCT_TBL.CREATED_DATE%TYPE;
V_MODIFIED_BY PRODUCT_TBL.MODIFIED_BY%TYPE;
V_MODIFIED_DATE PRODUCT_TBL.MODIFIED_DATE%TYPE;
BEGIN
F := UTL_FILE.FOPEN ('DATA_DIR', 'PRODUCT_TBLv51.csv', 'R');
IF V_ignore_headerlines > 0
THEN
BEGIN
FOR i IN 1 .. V_ignore_headerlines
LOOP
UTL_FILE.get_line(F, V_LINE);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
V_eof := TRUE;
END;
END IF;
WHILE NOT V_eof
LOOP
BEGIN
UTL_FILE.GET_LINE(F, V_LINE, 32767);
IF V_LINE IS NULL THEN
EXIT;
END IF;
V_PRD_ID := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 1, 'i', 1);
V_PATTERN := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 2, 'i', 1);
V_REMARK := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 12, 'i', 1);
V_CREATED_BY := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 13, 'i', 1);
V_CREATED_DATE := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 14, 'i', 1);
V_MODIFIED_BY := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 15, 'i', 1);
V_MODIFIED_DATE := REGEXP_SUBSTR(V_LINE, '([^,\(]*(\([^\)]*\)[^,\(]*)*)(,|$)', 1, 16, 'i', 1);
INSERT INTO PRODUCT_TBL (PRD_ID,PATTERN,REMARK,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE)
VALUES(V_PRD_ID, V_PATTERN, V_REMARK, V_CREATED_BY, V_CREATED_DATE, V_MODIFIED_BY, V_MODIFIED_DATE);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
v_error_code := SQLCODE;
v_error_message := SQLERRM;
dbms_output.put_line(v_error_code || SQLERRM);
EXIT;
END;
END LOOP;
COMMIT;
UTL_FILE.FCLOSE(F);
EXCEPTION
WHEN UTL_FILE.INVALID_OPERATION THEN
UTL_FILE.FCLOSE(F);
dbms_output.put_line('File could not be opened or operated on as requested.');
END;
/
add an EXCEPTION ... OTHER Block after the UTL_FILE part and see what kind of Exceptions actually go throuhg to catch them.
EXCEPTION
WHEN UTL_FILE.INVALID_OPERATION THEN
UTL_FILE.FCLOSE(F);
dbms_output.put_line('File could not be opened or operated on as requested.');
WHEN OTHERS THEN
dbms_output.put_line('other trouble'||SQLCODE||SQLERRM);
When you know which one happened you will know how to catch it.