How can I plug in this simple SQL logic to an already existing script correctly? - etl

Currently I am working on a sql script in informatica powercenter, the current script is this
ltrim(rtrim(Sur_Co))||
ltrim(rtrim(Seqe_N))||
rpad(ltrim(rtrim(Ct_Num)),30,' ')||
**iif(isnull(ltrim(rtrim(ent_Tx_ID))),rpad(' ',20,' '),
rpad(ltrim(rtrim(ent_Tx_ID)),20,' '))||**
**iif(isnull(ltrim(rtrim(ent_Tx_ID_Q))),rpad(' ',2,' '),
rpad(ltrim(rtrim(ent_Tx_ID_Q)),2,' '))||**
ETC
I dont need the iif is null logic for ent_Tx_ID and ent_Tx_ID_Q anymore,
I just need to always give the value of empty spaces for these two columns specifically like this,
**ent_Tx_ID = RPAD(' ',20,' ')
ent_Tx_ID_Q = RPAD(' ',2,' ')**
how would I edit this logic into that script?

You can use below concatenations directly in informatica,
ltrim(rtrim(Sur_Co))||
ltrim(rtrim(Seqe_N))||
rpad(ltrim(rtrim(Ct_Num)),30,' ')||
' 20 ' || ' 30 '

Related

Oracle PL/SQL Query With Dynamic Parameters in Where Clause

I'm trying to write a dynamic query that could have a different amount of parameters of different type. The only issue I'm having is handling if the value is a string therefore needing single quotes around it. I am using the value of a field called key_ref_ to determine what my where clause will look like. Some examples are:
LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^
PART_NO=221091^PART_REV=R02^
At the moment I am replacing the '^' with ' and ' like this:
REPLACE( key_ref_, '^' ,' and ' );
Then I'm trying to create the dynamic query like this:
EXECUTE IMMEDIATE
'select '||column_name_||' into column_ from '||base_table_||' where '||
key_ref_ || 'rownum = 1';
This won't work in cases where the value is not a number.
Also I only added "rownum = 1" to handle the extra 'and' at the end instead of removing the last occurence.
If the input will not have the tild symbol(~) then you can try the below code.
if the input has tild, you can replace it with some other value which should not be there in input
considering the input provided in the example..
LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^PART_NO=221091^PART_REV=R02^
use the below code
replace(replace(replace('LINE_NO=1^ORDER_NO=P6002277^RECEIPT_NO=1^RELEASE_NO=1^PART_NO=221091^PART_REV=R02^','^','~ and '),'=','=~'),'~',q'[']')
and the result would be
LINE_NO='1' and ORDER_NO='P6002277' and RECEIPT_NO='1' and RELEASE_NO='1' and PART_NO='221091' and PART_REV='R02' and
System will type cast the number fields so, there would not be any issue.

Oracle LTRIM triming too many charecters

I am trying to use LRTIM to obtain part of a string.
This is the code that I am using.
SELECT
C.MANUFACTURER,
C.MODEL_GROUP,
LTRIM(C.VARIANT, C.MANUFACTURER || ' ' || C.MODEL_GROUP), "VAR DESC"
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C
This is what I have field wise
VARIANT
AUDI A3 DIESEL SPORTBACK 2.0 TDI SE Technik 5dr Manual 150
MANUFACTURER
AUDI
MODEL_GROUP
A3
This is the result that I am getting
MANUFACTURER MODEL_GROUP VAR DESC
AUDI A3 ESEL SPORTBACK 2.0 TDI SE Technik 5dr Manual 150
It is cutting off the first 2 characters of DIESEL, the problem is that this is not happening in all cases. Sometimes it is cutting off 1 character and sometimes none!
I have considered other methods of achieving this, i.e. looking for the spaces, but have other manufacturers and model groups that have multiple spaces so was trying to subtract the MANUFACTURER and MODEL_ROUP from the VARIANT field (string)
Any ideas as to why this might be happening or other suggestions for achieving this would be greatly appreciated.
Many thanks in advance,
Keith
Apologies seems like the editor is reformating the results that I am getting
Oracle query result
You can use REGEXP_REPLACE with '^' - Indicating start of string.
SELECT
C.MANUFACTURER,
C.MODEL_GROUP,
REGEXP_REPLACE(C.VARIANT, '^'||C.MANUFACTURER || ' +' || C.MODEL_GROUP) "VAR DESC",
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C
The second parameter to LTRIM is a set of single characters, not a sequence of characters. In your example the database removes characters from the left until it encounters the first character not in ('A','U','D','I','A','3'), which is the E of DIESEL.
In order to remove a prefix p from a string s use something like
case when SUBSTR(s,1,LENGTH(p))=p then SUBSTR(s,LENGTH(p)+1) else s end
which in your case expands to
case when SUBSTR(C.VARIANT, 1, LENGTH(C.MANUFACTURER || ' ' || C.MODEL_GROUP))
= C.MANUFACTURER || ' ' || C.MODEL_GROUP
then SUBSTR(C.VARIANT, 1+ LENGTH(C.MANUFACTURER || ' ' || C.MODEL_GROUP))
else s
end

Oracle/ PLSQL condition like 'string' || '%'

Why this
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen' || '%'
as the same
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen%'
How does the first one work?
|| is concatenation operator. Oracle will first perform concatenation and then will use LIKE to match the pattern. Hence operationally it will be same as the second one.
However you should use the second one as it will be more efficient in performance and easy to read.
First one has extra overhead to append two strings before using LIKE to match the pattern.
the first one catenate 'Nguyen' and '%' through the pipes '||' in first place.
Because you don't have any space like 'Nguyen ' or ' %', it's the same as 'Nguyen%'.
There is no difference between these two:
Here the double pipe(||) is just a concatenation of expression.
Before db evaluate against like parameter, it concatenates
Hence both of those are same.
Both are same, as || operator concats 'Nguyen' and '%', it will be more helpful if you want to concat parameterize variable like below
SELECT * FROM STUDENT
WHERE FULLNAME LIKE :name || '%'

oracle regexp_replace delete last occurrence of special character

I have a pl sql string as follows :
String := 'ctx_ddl.add_stopword(''"SHARK_IDX19_SPL"'',''can'');
create index "SCOTT"."SHARK_IDX2"
on "SCOTT"."SHARK2"
("DOC")
indextype is ctxsys.context
parameters(''
datastore "SHARK_IDX2_DST"
filter "SHARK_IDX2_FIL"
section group "SHARK_IDX2_SGP"
lexer "SHARK_IDX2_LEX"
wordlist "SHARK_IDX2_WDL"
stoplist "SHARK_IDX2_SPL"
storage "SHARK_IDX2_STO"
sync (every "SYSDATE+(1/1)" memory 67108864)
'')
/
';
I have to get search the final occurrence of '/' and add ';' to it. Also I need to escape the quotes preset in parameters ('') to have extra quotes. I need output like
String := 'ctx_ddl.add_stopword(''"SHARK_IDX19_SPL"'',''can'');
create index "SCOTT"."SHARK_IDX2"
on "SCOTT"."SHARK2"
("DOC")
indextype is ctxsys.context
parameters(''''
datastore "SHARK_IDX2_DST"
filter "SHARK_IDX2_FIL"
section group "SHARK_IDX2_SGP"
lexer "SHARK_IDX2_LEX"
wordlist "SHARK_IDX2_WDL"
stoplist "SHARK_IDX2_SPL"
storage "SHARK_IDX2_STO"
sync (every "SYSDATE+(1/1)" memory 67108864)
'''')
/;
';
Any help.
There's an age-old saying: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems"
Unless you are confronted by a problem that truly requires regular expressions, I'd recommend working with basic string manipulation functions.
Semi-colon:
Use INSTR to find last occurence of '/', call this P1.
Result = Substr from position 1 through P1||';'||substr from P1+1 through to end-of-string
Parameters substitution:
Use INSTR to find where parameter list starts (i.e. find "parameters(" in your string) and ends (presumably the last closing parenthesis ")" in your string). Call these P2 and P3.
Result = substr from 1 through P2 || REPLACE(substr from P2+1 through P3-1,'''','''''''') || substr from P3 to end-of-string

Read using CSVREAD with non-printing characters as field and record separators

I have a file that I would like to read in H2 that uses FIELD(ASCII code 31) & RECORD(ASCII code 30) as the field and record separators in my file. I've tried this but it's not working...
SELECT * FROM CSVREAD('test.csv', null, 'rowSeparator=' || CHAR(30) || 'fieldSeparator=' || CHAR(31));
How do I need to format this to read from my file?
EDIT I
This parses the fields out correctly but the rows aren't being parsed out...not sure why:
SELECT * FROM CSVREAD('C:\Users\zmacomber\ReceiptPrinter\data\bak\address.dat', null, STRINGDECODE('charset=UTF-8 rowSeparator=' || CHAR(30) || ' fieldSeparator=' || CHAR(31)));
Looking at the source code of the CSV tool, unfortunately you can not currently change the row separator used for reading (parsing). The row separator is only used for writing, not for reading. For reading, you would need to use \n, \r, or a combination of both.
I understand this is unexpected, but that's the way it currently is.

Resources