IBM Db2: select numeric characters only from a column - sql

I have a column 'TEST_COLUMN' that carries 3 values:
123
123ad(44)
w-eq1dfd2
I need to SELECT TEST_COLUMN but get the following result:
123
12344
12
I am running on Db2 Warehouse on Cloud.

You can use REGEXP_REPLACE:
SELECT REGEXP_REPLACE(
'123Red345', '[A-Za-z]','',1)
FROM sysibm.sysdummy1
The query would return "123345".
Because you asked below, here is the generic version:
SELECT REGEXP_REPLACE(YOUR_COLUMN, '[A-Za-z]','',1)
FROM SCHEMA.TABLE

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')

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

making comma separated string in Oracle

How can we convert 12345 into 1,2,3,4,5 .
I can do the reverse by using replace command and i can replace comma by null. But I am not able to do the above. Could you please help on the same.
Thanks in Advance
You can use regexp_replace():
rtrim(regexp_replace('12345', '([0-9])', '\1,'), ',')
The rtrim() is necessary because the last digit is also replaced.
Online example: https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=724adecda03305b281ad3bf0f380ca58
If you have a fixed template with consecutive, one-digit numbers like 1234 or 12345 or 123456789
you may try the following by using listagg function of Oracle :
with t as
(
select '12345' as col from dual
)
select listagg(level,',') within group (order by level) as str
from t
connect by level <= length(col);
STR
---------
1,2,3,4,5
SQL Fiddle Demo 1
OR
For more generalized solution, use the following :
with t as
(
select 'abcde' as col from dual
)
select listagg(substr(col,level,1),',') within group (order by level) as str
from t
connect by level <= length(col);
STR
---------
a,b,c,d,e
SQL Fiddle Demo 2

How to remove only letters from a string in BigQuery?

So I'm working with BigQuery SQL right now trying to figure out how to remove letters but keep numeric numbers. For example:
XXXX123456
AAAA123456789
XYZR12345678
ABCD1234567
1111
2222
All have the same amount of letters in front of the numbers along with regular numbers no letters. I want the end result to look like:
123456
123456789
12345678
1234567
1111
2222
I tried using PATINDEX but BigQuery doesn't support the function. I've also tried using LEFT but that function will get rid of any value and I don't want to get rid of any numeric value only letter values. Any help would be much appreciated!
-Maykid
You can use regexp_replace():
select regexp_replace(str, '[^0-9]', '')
Below example is for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'XXXX123456' str UNION ALL
SELECT 'AAAA123456789' UNION ALL
SELECT 'XYZR12345678' UNION ALL
SELECT 'ABCD1234567' UNION ALL
SELECT '1111' UNION ALL
SELECT '2222'
)
SELECT str, REGEXP_REPLACE(str, r'[a-zA-Z]', '') str_adjusted
FROM `project.dataset.table`

extracting text from a column using regexp_substr

I have a table with a varchar column with data like this:
"<tasa>
<parametros>
<parametro>
<nombre>ea</nombre>
<valor>35</valor>
</parametro>
</parametros>
<valorTasa>3.15</valorTasa>
</tasa>"
I need to be able to extract the value between the valorTasa tags, but don't know how to use the function and can't access oracle documentation.
I'm trying something like
select regexp_substr(field, '<valorTasa>[0-9]{0-3}</valorTasa') from dual;
With no results.
Any help would be greatly appreciated
More simple way would be using extractvalue function to extract the value of the node.
-- sample of data
SQL> with t1(col) as(
2 select '<tasa>
3 <parametros>
4 <parametro>
5 <nombre>ea</nombre>
6 <valor>35</valor>
7 </parametro>
8 </parametros>
9 <valorTasa>3.15</valorTasa>
10 </tasa>'
11 from dual
12 )
13 select extractvalue(xmltype(col), '/tasa/valorTasa') as res
14 from t1
15 /
RES
-------
3.15
Actually REGEXP_REPLACE will work best for this. If you put a part of the search expression in parentheses you can refer to it in the third "replace-with" parameter - the first such expression is \1, the second is \2, and so on up to \9 (you can't do more than 9).
For your requirement, try this:
SELECT REGEXP_REPLACE(myXMLCol, '^.*<valorTasa>(.*)</valorTasa>.*$', '\1') FROM myTable
^^^^ ^^
The part in the parentheses above - (.*) maps to \1. The Oracle REGEXP_REPLACE docs explain this better than I can :)
SELECT regexp_replace(
regexp_substr(field, '<valorTasa>[0-9\.]+</valorTasa>'),
'<valorTasa>([0-9\.]+)</valorTasa>',
'\1')
from dual;
For multiline XML documents, as we have here, regexp_replace routine could be used but only with correct match_parameter = mn :
with t1(col) as(
select '<tasa>
<parametros>
<parametro>
<nombre>ea</nombre>
<valor>35</valor>
</parametro>
</parametros>
<valorTasa>3.15</valorTasa>
</tasa>'
from dual
)
select
REGEXP_REPLACE(col, '^.*<valorTasa>(.*)</valorTasa>.*$', '\1', 1, 0, 'mn') as res
from t1
/