best way to remove '-' in oracle SP - oracle

I have an Oracle stored procedure, that has as one of its parameters a Social Security Number (SSN) in the hyphenated format xxx-xx-xxxx. What is the best way to remove the hyphens, so that I transform the SSN to a xxxxxxxxx format?

REPLACE('xxx-xx-xxxx', '-', '')
Or as #jitter mentions, the third argument defaults to '', so:
REPLACE('xxx-xx-xxxx', '-')

To answer your second question, if you already have the xxx-xx-xxxx version, don't overwrite it and you'll have both.
If you're expecting xxxxxxxxx and you want xxx-xx-xxxx, piece it together using:
SUBSTR('xxxxxxxxx', 0, 3) || '-' || SUBSTR('xxxxxxxxx', 3, 2) || '-' || SUBSTR('xxxxxxxxx', 5, 4)

mystring := v_mySSN;
select substr(mystring, 0, 3)||'-'||substr(mystring, 3, 2)||'-'||substr(mystring, 5, 4)
into varialble
from some_table;
This would work inside a procedure to select your value in a variable correctly.

Related

How to extract specific values in a string in informatica

I need to extract the (, ), - and numbers from 0 to 9 from the input field phone number.
For eg., for input data : (891)456-567A$rT67
Output should be (891)456-56767
can be done in below 2 steps -
v_ph_no = REG_REPLACE( in_ph_no,'[^0-9]','')` -- variable port that will replace everything but 0 to 9.
out_ph_no = '(' || substr(v_ph_no,1,3)||')' || substr (v_ph_no,4,3)||'-' || substr (v_ph_no,6) -- o/p port that will create ph no in desired format.
Explanation - first remove all unwanted characters and then format it in desired phone no format.

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

Invalid Number In substr

I Used substring for this query
to_number(substr(skidno,instr(skidno,'/')+1,length(skidno)))
this i'll pass for ths query Skid no 'MMM-1718/000325 / 1' i got Invalid Number
If your need is to extract the part after the 2nd occurrence of "/"; the following query will suffice your need ---
select to_number(trim('/' FROM
substr('MMM-1718/000325 / 1',
instr('MMM-1718/000325 / 1', '/', 1, 2))))
from dual;
You may want to use regexp_substr. If you want to only get the number at the end of the value:
to_number(regexp_substr(skidno, '\d+$'))
You cannot convert it to number because you will get "/" in the result.
Result: '000325 / 1'.
If you only require only the number between '/' then try below :
to_number(substr(skidno ,instr(skidno ,'/')+1,
instr(skidno ,'/',1,2) -instr(skidno ,'/',1,1)-1 ))
Since you need part of the string that begins after second occurrence of '/' character and goes to the end of the string, the following solution is probably the easiest using substr/instr:
with t as
(
select 'MMM-1718/000325 / 1' mystring
from dual
)
select to_number(substr(mystring, instr(mystring, '/', 1, 2) + 1))
from t;
Note: in this case you just have to define starting position in substr function; length of the string is not needed.

convert word to ascii and return to word adding some value

I am working on PL/SQL . The oracle password by developer is set such way
=> input word => converted to ascii => added 2 to each letter => converted back to word
ex: input password is "admin".
admin is splitted into characters/letters (a, d, m, i, n)
converted to ascii and added 2 and again converted to word
a=97 97+2 = 99 = c
d=100 100+2=102 = f
m=109 109+2=111 = o
i=105 105+2=107 = k
n=110 110+2=112 = p
what i did is
$pass=str_split('admin');
foreach($pass as $password){
$new_password[]=chr(ord($password)+2);
}
$final= $new_password[0].$new_password[1].$new_password[2].$new_password[3].$new_password[4]; //the values 0-4 is set manually
echo $final;
result: cfokp
But i could not get proper ans to run the result string on command and match the oracle password with the retrieved one.
Another way in SQL is to split the characters, add 2 to the ascii value, and aggregate the string.
Of course, it won't be faster than the TRANSLATE approach. But, for a single or small set of values it shouldn't matter much.
For example,
SQL> WITH data AS
2 (SELECT 'admin' str FROM dual
3 )
4 SELECT str, LISTAGG(CHR(ASCII(REGEXP_SUBSTR(str, '\w', 1, LEVEL)) + 2), '') WITHIN GROUP(
5 ORDER BY LEVEL) str_new
6 FROM data
7 CONNECT BY LEVEL <= LENGTH(str)
8 /
STR STR_NEW
------ -------
admin cfokp
SQL>
The above SQL does following important tasks:
Split the string into characters using REGEXP_SUBSTR and ROW GENERATOR technique
Add value 2 to the ascii value of each character.
Convert back the modified ascii into characters.
Aggregate the string using LISTAGG
This is probably easier to do with translate:
select translate('admin',
'abcdefghijklmnopqrstuvwxyz',
'cdefghijklmnopqrstuvwxyzab'
)
from dual;
I'm not sure what you want to do with "y" and "z". This maps them back to "a" and "b". You can extend this to upper case letters and other characters if you need.

Oracle Pattern matching

In Oracle I want to check whether the string has "=' sign at the end. could you please let me know how to check it. If it has '=' sign at the end of string, I need to trailing that '=' sign.
for eg,
varStr VARCHAR2(20);
varStr = 'abcdef='; --needs to trailing '=' sign
I don't think you need "pattern matching" here. Just check if the last character is the =
where substr(varstr, -1, 1) = '='
substr when called with a negative position will work from the end of the string, so substr(varstr,-1,1) extracts the last character of the given string.
Use the REGEX_EXP function. I'm putting a sql command since you didn't specify on your question.:
select *
from someTable
where regexp_like( someField, '=$' );
The pattern $ means that the precedent character should be at the end of the string.
see it here on sql fiddle: http://sqlfiddle.com/#!4/d8afd/3
It seems that substr is the way to go, at lease with my sample data of about 400K address lines this returns 1043 entries that end in 'r' in an average of 0.2 seconds.
select count(*) from addrline where substr(text, -1, 1) = 'r';
On the other hand, the following returns the same results but takes 1.1 seconds.
select count(*) from addrline where regexp_like(text, 'r$' );

Resources