Netezza string comparison - sql

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.

Related

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

How to replace characters at specific position in several words using REGEX_REPLACE

I have a query similar to this:
SELECT YEAR_CODE FROM YEAR_CODES
and it returns several records: typically 1 but sometimes 2 or 3. The returned records look like this: 2018FOO, 2019BAR
I need to get the matching previous year of the returned codes. For instance:
2018FOO becomes 2017FOO
2019BAR becomes 2018BAR
Looking for something similar to:
REGEX_REPLACE(SELECT YEAR_CODE FROM YEAR_CODES, 4th character, 4th character minus 1)
You don't need regexp_replace(), using substr() string operator with concat() function (or concatenation operators ||) is enough :
with year_codes(year_code) as
(
select '2018FOO' from dual union all
select '2019BAR' from dual
)
select concat(substr(year_code,1,4) - 1,substr(year_code,-3)) as year_code
from year_codes;
YEAR_CODE
---------
2017FOO
2018BAR
to_number() conversion is redundant, since Oracle implicitly considers a string as a number which is completely composed of digits for an arithmetic operation.
You can do use string operations:
with c as (
<your query here>
)
select
from year_code yc
where to_number(substr(yc.code, 1, 4)) = to_number(substr(c.code)) - 1 and
substr(yc.code, 5) = substr(c.code, 5)

Oracle regular expression to replace numbers with alphabets

I have a string which contains only numbers.
I need to replace all digits in the string with a corresponding alphabet as below,
0 -> A
1 -> B
2 -> C
..
9 -> J
I tried with below using translate and replace functions and it works fine for me,
Forward :
WITH T (ID) AS (SELECT '10005614827' FROM DUAL)
SELECT ID, TRANSLATE(ID,'0123456789','ABCDEFGHIJ') "TRANSLATE",
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ID,'0','A'),'1','B'),'2','C'),'3','D'),'4','E'),'5','F'),'6','G'),'7','H'),'8','I'),'9','J') "REPLACE"
FROM T;
Output:
ID TRANSLATE REPLACE
10005614827 BAAAFGBEICH BAAAFGBEICH
Reverse:
WITH T (ID) AS (SELECT 'BAAAFGBEICH' FROM DUAL)
SELECT ID, TRANSLATE(ID,'ABCDEFGHIJ','0123456789') "TRANSLATE",
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(ID,'A','0'),'B','1'),'C','2'),'D','3'),'E','4'),'F','5'),'G','6'),'H','7'),'I','8'),'J','9') "REPLACE"
FROM T;
Output:
ID TRANSLATE REPLACE
BAAAFGBEICH 10005614827 10005614827
Is there any way to use regular expression to implement this?
WITH T (ID) AS (SELECT '10005614827' FROM DUAL)
SELECT ID, REGEXP_REPLACE(ID,'[0-9]','[A-J]')
FROM T;
It's not possible in Oracle because of the limitations of current implementation.
More specifically, you cannot apply a function to matched value, you only can use backreferences in a form \n where n is a digit from 1 to 9.
For example, you can match each digit and repeat it as many times as it equals to.
column example format a40
with t(id) as (select '10005614827' from dual)
select id,
regexp_replace(id,'(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)|(0)','\1\2\2\3\3\3\4\4\4\4\5\5\5\5\5\6\6\6\6\6\6\7\7\7\7\7\7\7\8\8\8\8\8\8\8\8\9\9\9\9\9\9\9\9\9') example
from t
/
ID EXAMPLE
----------- ----------------------------------------
10005614827 1555556666661444488888888227777777
1 row selected.
But you can't apply any function to \n in replacing string.
On the other hand, in languages like Perl, Java, Scala... or even in PowerShell and others it's doable.
Example from Scala REPL.
scala> val str = "10005614827"
str: String = 10005614827
scala> // matching and converting each digit separately
scala> "\\d".r.replaceAllIn(str, x => (x.group(0)(0)+17).toChar + "")
res0: String = BAAAFGBEICH
scala> // marching and converting sequences of digits
scala> "\\d+".r.replaceAllIn(str, x => x.group(0).map(x=>(x+17).toChar))
res1: String = BAAAFGBEICH
To complete the picture, model solution just for fun.
SQL> with t(id) as (select '10005614827' from dual)
2 select *
3 from t
4 model partition by (id) dimension by (0 i) measures (id result)
5 rules iterate(10)
6 (result[0] = replace(result[0],iteration_number,chr(ascii(iteration_number)+17)))
7 /
ID I RESULT
----------- ---------- -----------
10005614827 0 BAAAFGBEICH
translate is the very best approach in this case though. This is exactly what this function was made for.
PS. Scala equivalent for example above with a function applied to matched value instead of using backreferences.
"\\d".r.replaceAllIn(str, x => (x.group(0)*(x.group(0)(0)-48)))
Using translate function I do not see problems.
Having for example the string number '3389432543' you can convert it using
SELECT TRANSLATE('3389432543','0123456789','ABCDEFGHIJ')
FROM DUAL;

Replace part of string in table column

Using Remove part of string in table as an example, I want to change part of my string in my database column with a different string.
Ex:
Database says E:\websites\nas\globe.png , E:\websites\nas\apple.png and etc
I want it to say \\nas\globe.png, \\nas\apple.png,
Only part I want to replace is the E:\websites\ not the rest of the string
How do I do this?
So far I have:
SELECT file_name,
REPLACE(file_name,'E:\websites\','\\nas\')
FROM t_class;
I just referenced http://nntp-archive.sybase.com/nntp-archive/action/article/%3C348_1744DC78C1045E920059DE7F85256A8B.0037D71C85256A8B#webforums%3E
and used:
SELECT REPLACE('E:\websites\web\Class\Main_Image\','E:\websites\web\Class\Main_Image\','\\nas\class_s\Main_Image\') "Changes"
FROM DUAL;
but once again it wouldn't change O.o
In Oracle, you may need to double up on the back slashes:
SELECT file_name,
REPLACE(file_name,'E:\\websites\\', '\\\\nas\\')
FROM t_class;
For the fun of it, using regexp_replace:
SQL> with tbl(filename) as (
2 select 'E:\websites\nas\globe.png' from dual
3 union
4 select 'E:\websites\nas\apple.png' from dual
5 )
6 select filename, regexp_replace(filename, 'E:\\websites', '\\') edited
7 from tbl;
FILENAME EDITED
------------------------- --------------------
E:\websites\nas\apple.png \\nas\apple.png
E:\websites\nas\globe.png \\nas\globe.png
SQL>
I found a reference at how to replace string values of column values by SQL Stored Procedure
by doing the following:
UPDATE t_class SET file_name =
REPLACE
(file_name, 'E:\websites\web\Class\Main_Image\No_Image_Available.png', '\\nas\class_s\Main_Image\No_Image_Available.png');
so only difference is the update and = sign

PLSQL Getting values from string

I have this varchar2(2000) string:
id=100\nid2=0\nid3=0\dtext='more Text'
and I want to get only the values e.g. more Text or 0 (id3).
I was trying to use a customized SPLIT function, where separator is \n but this only returns me for example id3=0 (in this case I need '0' as result).
How can I do this more efficient?
and I want to get only the values e.g. more Text.
Simply use SUBSTR and INSTR
SQL> WITH DATA AS
2 ( SELECT q'[id=100\nid2=0\nid3=0\dtext='more Text']' str FROM dual
3 )
4 SELECT SUBSTR(str,instr(str, '''')+1,LENGTH(SUBSTR(str,instr(str, '''')))-2) str
5 FROM DATA
6 /
STR
---------
more Text
SQL>
You could get all the values with something like this:
WITH DATA AS
(SELECT q'[id=100\nid2=0\nid3=0\ndtext='more Text']' str FROM dual)
SELECT replace(substr(regexp_substr(str,'(=.+?\n)|(=.+?$)',1,level),2),'\n') v
FROM DATA
CONNECT BY LEVEL <= LENGTH(regexp_replace(str,'([^=]+=.+?\n)|([^=]=.+?$)'))