Regular Expression to parse 112^3^1^1^ [duplicate] - sql

This question already has answers here:
How to parse data using REGEXP_SUBSTR?
(3 answers)
Making columns from a string in oracle
(1 answer)
Parsing pipe delimited, irregular strings in Oracle
(2 answers)
Closed 4 years ago.
I need to parse a string like 112^3^1^1^ into separate values.
The first ('112') is order_no, second one is line_no, third one is release_no, and the fourth one is receipt_no.
What kind of regex can I use in Oracle?

with demo (str) as
( select '112^3^1^1^' from dual )
select str
, regexp_substr(str,'[^^]+',1,1) as order_no
, regexp_substr(str,'[^^]+',1,2) as line_no
, regexp_substr(str,'[^^]+',1,3) as release_no
, regexp_substr(str,'[^^]+',1,4) as receipt_no
from demo;
STR ORDER_NO LINE_NO RELEASE_NO RECEIPT_NO
---------- ---------- --------- ------------ ----------
112^3^1^1^ 112 3 1 1
This depends on each component having a value, because the pattern it is using is 'one or more characters that are not ^'. It would need a bit of tweaking if a value was missing, e.g. '112^3^^1^' (no release_no).

Related

Need Regex pattern for right side of 10 digits from mobile Number

I need help with how to write a regex for getting the last 10 digits from the right side of the mobile Number
For examples:
Input is: 919345678901
output is: 9345678901
input2 is: 09934567892
output is: 9934567892
PL/SQL means Oracle; in that case, you don't need slow regular expressions as fast substr function does the job nicely:
Sample data:
SQL> with test (col) as
2 (select '919345678901' from dual union all
3 select '09934567892' from dual
4 )
Query begins here:
5 select col,
6 substr(col, -10) result
7 from test;
COL RESULT
------------ ----------------------------------------
919345678901 9345678901
09934567892 9934567892
SQL>
regexp_replace(target,'^\d*(\d{10})$', '\1')

how to get the string before first occurrence of a special character [duplicate]

This question already has answers here:
How to Select a substring in Oracle SQL up to a specific character?
(8 answers)
Closed 1 year ago.
i have a column containing hostnames in the format of :
oraclehost.server.region.company.net
How to extract the oraclehost part from the hostname i.e the string before the first ..
Please sugges.Thanks.
SELECT REGEXP_SUBSTR(HOSTNAMES, '[^.]+', 1, 1) FROM MYTABLE;
Alternatively, substr + instr combination which would probably perform better for large data sets:
substr(hostnames, 1, instr(hostnames, '.') - 1)
For example:
SQL> with mytable (hostnames) as
2 (select 'oraclehost.server.region.company.net' from dual)
3 select substr(hostnames, 1, instr(hostnames, '.') - 1) result
4 from mytable;
RESULT
----------
oraclehost
SQL>

Capturing particular part of Integer Value from part of a String value

I have a table like cust_attbr consists column attbr which has values like:
{"SRCTAXAMT":"11300",เอ็ก10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}
{"SRCTAXAMT":"11300", กรุงค10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}
........ ... ...
{"SRCTAXAMT":"11300", กรุงค10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":" "}
So, I have to write one select statement which will fetch only VAT_NUMBER value like:
0835546003122
0835546003122
.... ... ..
null
With sample data you posted:
SQL> select * From test;
ID ATTBR
---------- ----------------------------------------------------------------------------------------------------------------
1 "{"SRCTAXAMT":"11300",????10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}"
2 "{"SRCTAXAMT":"11300", ?????10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}"
3 "{"SRCTAXAMT":"11300", ?????10110","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":" "}"
this might be one option:
SQL> select id,
2 regexp_substr(regexp_substr(attbr, 'VAT_NUMBER":"(\d+)?'), '\d+$') vat
3 from test;
ID VAT
---------- --------------------
1 0835546003122
2 0835546003122
3
SQL>
Inner regexp_substr returns VAT_NUMBER followed by optional number, while the outer one extracts only the number anchored to the end of the previous substring.
If you're on 18c and the data is actual json (it currently is not because of the double quotes around the curly braces and the ",.กรุงค10110" - It is unclear that this is because of your sample data) you could use json_table function:
WITH t (json_val) AS
(
SELECT '{"SRCTAXAMT":"11300","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}' FROM DUAL UNION ALL
SELECT '{"SRCTAXAMT":"11300","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":"0835546003122"}' FROM DUAL UNION ALL
SELECT '{"SRCTAXAMT":"11300","TAXAMT":"11300","LOGID":"190301863","VAT_NUMBER":" "}' FROM DUAL
)
SELECT jt.*
FROM t,
JSON_TABLE(json_val, '$'
COLUMNS (first_name VARCHAR2(50 CHAR) PATH '$."VAT_NUMBER"')) jt;
0835546003122
0835546003122
One option would be converting those column values to JSON syntax an then extract the values of VAT_NUMBER keys provided DB version is 12c Release 1+. Here, we have an issue that there are unrecognized characters, probably an alphabet from far east and those strings are not properly quoted, then we need to remove the part upto TAXAMT key, and then extracting VAT_NUMBER key's value through prefixing an opening curly brace('{') by use of JSON_VALUE() function :
SELECT JSON_VALUE(
'{'||REGEXP_REPLACE(str,'(.*10110",)(.*)','\2'),
'$.VAT_NUMBER'
) AS VAT_NUMBER
FROM tab --> your original data source
Demo

Can (should) I include elements of TO_CHAR, CONCAT and FM in the same select statement?

I'm following up on a previous post that successfully generated an Oracle SELECT statement. From within that prior script I now need to
concatenate two different fields (numeric values for 3-digit area codes and 7-digit phone numbers), then
format the resulting column as XXX-XXX-XXXX
but my attempts at using TO_CHAR, CONCAT (or || I have tried doing the concatenation both ways), and FM in the same line result in invalid number or invalid operator errors (depending on how I've rearranged the elements in the line) painfully reminding me that my barely-elementary scripting shows a significant lack understanding of proper use and syntax.
The combination of TO_CHAR and CONCAT (||) successfully produces a 9-digit string, but I'm trying to attain as result formatted as XXX-XXX-XXXX from the following (I've edited out the lines from the original script for data elements not relevant to this particular question; nothing in the original query is nested, it just selects several fields and has a series of left joins linking on a common UID field in different tables)
select distinct
cn.dflt_id StudentIdNumber,
to_char (p.area_code || p.phone_no) Phone,
from
co_name cn
left join co_v_name_phone1 p on cn.name_id = p.name_id
order by cn.dflt_id
Would anyone offer helpful advice on attaining the desired XXX-XXX-XXXX formatting in the resulting Phone column? My attempts with variants of 'fm999g999g9999' have thus far not been successful.
Thanks,
Scott
Here are a few options that crossed my mind; have a look, pick the one you find the most appropriate. If you still have problems, post your own test case.
RES2 is a simple concatenation of substrings that have a - in between
RES3 uses format mask with an adjusted NLS_NUMERIC_CHARACTERS for thousands
RES4 concatenates area code (which is OK by itself) with regular expression that splits a string into two parts; the first has {3} characters, and the second one has {4} of them
By the way, are area codes really numbers? No leading zeros?
SQL> with test (area_code, phone_number) as
2 (select 123, 9884556 from dual union
3 select 324, 1254789 from dual
4 )
5 select
6 to_char(area_code) || to_char(phone_number) l_concat,
7 --
8 substr(to_char(area_code) || to_char(phone_number), 1, 3) ||'-'||
9 substr(to_char(area_code) || to_char(phone_number), 4, 3) ||'-'||
10 substr(to_char(area_code) || to_char(phone_number), 7)
11 res2,
12 --
13 to_char(to_char(area_code) || to_char(phone_number),
14 '000g000g0000', 'nls_numeric_characters=.-') res3,
15 --
16 to_char(area_code) ||'-'||
17 regexp_replace(to_char(phone_number), '(\d{3})(\d{4})', '\1-\2') res4
18 from test;
L_CONCAT RES2 RES3 RES4
------------- ------------- ------------- -------------
1239884556 123-988-4556 123-988-4556 123-988-4556
3241254789 324-125-4789 324-125-4789 324-125-4789
SQL>

Netezza string comparison

I have 2 columns string and Character. My requirement is to compare those 2, if the second column(char) is available in first column(string) the answer should be some value(say 1).
I tried using translate but it doesn't work.
Any functions available for above requirement.
Netezza implements the usual ANSI standard position function.
TESTDB.ADMIN(ADMIN)=> select position('BC' in 'ABCDEF');
STRPOS
--------
2
(1 row)
TESTDB.ADMIN(ADMIN)=> select position('BZ' in 'ABCDEF');
STRPOS
--------
0
(1 row)
The translate function converts certain characters in a source string, so that won't do what you want at all.
TESTDB.ADMIN(ADMIN)=> select translate('ABCDE', 'BE', 'XYZ');
TRANSLATE
-----------
AXCDY
(1 row)
select
case when
(select
STRPOS(
trim(lower('abc.com')),
trim(lower('xyz'))
)
) > 0
then
1
else
2
end;
To test this code put your first string instead of 'abc.com' and second string at 'xyz'. If your second string present in first string it will output 1 else 2.
Hope this will help.