Oracle: remove first 4 characters from a string - sql

So I want to remove the first 4 characters from a string in oracle. Those characters can be different every time.
In my case I need to take away the first 4 characters of an IBAN and put them at the end of the string. I got the part of putting them to the end of the string but I can't get the first 4 characters to be removed. Every solution I find on the internet removes specified characters, not characters from a certain position in the string (1 to 4).
I used the code below to get the first 4 characters to the end of the string and wanted to try something similar for removing them at the front but without success.
SELECT SUBSTR(iban_nummer, 1, 4) INTO iban_substring FROM dual;
iban_nummer := iban_nummer || iban_substring;

See the docs:
substring_length ...
When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify
a value that is less than 1, the function returns NA.
So iban_nummer := substr(iban_nummer, 5) || substr(iban_nummer, 1,4) should work. The first part selects all characters beginning from the 5th, the second character numbers 1..4.

update table_name set col_name=substr(col_name,5);

try regexp, like:
SELECT regexp_replace(t.iban_nummer,'(.{4})(.*)','\2\1') FROM t;

Alternative way using regexp :
SELECT regexp_replace(t.iban_nummer,'^.{4}(.*)','\2\1') FROM dual;

Related

Inserting special characters inside a string without replacing any character

Suppose I have a varchar variable.Can I insert different symbols after evry 2 character of the string untill end of the string .i.e untill length (string).
For example:
Input: '12345678'
And we don't know what is input and length of input and the output we want is :
Output: '12_34&56#78' (special character can be anything )
Please let me know if it possible doing dynamically using loop or something.
This can be done using a simple REGEXP_REPLACE.
The first argument will be your original string.
The second argument will be your regex pattern you want to put a a character after. In this case it is . (any character) occurring 2 times. It is surrounded by parentheses which will be needed for the next argument to specify a capture group.
The third argument is using \1 to specify the first capture group, then you can put anything you'd like to appear after the capture group. In the example below I used a !.
SELECT REGEXP_REPLACE ('12345678', '(.{2})', '\1!') as str FROM DUAL;
STR
_______________
12!34!56!78!
If you do not want the character at the end of your string, you can TRIM it from the right side or SUBSTR if the special character may appear at the end of the original string.
You can use an hierarchical query during the split and padding a special character through use of DBMS_RANDOM.VALUE in order to produce some special characters except for the last piece, and then LISTAGG() function to combine the pieces back again such as
SELECT id,LISTAGG( SUBSTR(col,1+(level-1)*2,2)||
CASE WHEN level < CEIL(LENGTH(col)/2)
THEN
CHR(TRUNC(DBMS_RANDOM.VALUE(33,48)))
END)
WITHIN GROUP (ORDER BY level)
AS result
FROM t
GROUP BY id
CONNECT BY level <= CEIL(LENGTH(col)/2)
AND PRIOR SYS_GUID() IS NOT NULL
AND PRIOR ID = ID
Demo

Extract Specific Set of data from a String in Oracle

I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'. I am trying to remove the _1_2_3_4_5 & the 1_ portion from the string. Which is essentially the numeric portion in the string. any special characters after the last alphabet must also be removed. In this example the _ after the character E must also not be present.
and the Query I am trying is as below
SELECT
REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+',1,1)
from dual
The Data I get from the above query is as below: -
_A_B_C_D_E_
I am trying to figure a way to remove the underscore towards the end. Any other way to approach this?
Assuming the "letters" come first and then the "digits", you could do something like this:
select regexp_substr('A_B_C_D_E_1_2_3_4_5','.*[A-Z]') from dual;
This will pull all the characters from the beginning of the string, up to the last upper-case letter in the string (.* is greedy, it will extend as far as possible while still allowing for one more upper-case letter to complete the match).
I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'
Use REGEXP_REPLACE:
SQL> SELECT trim(BOTH '_' FROM
2 (REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[0-9]+', ''))) str
3 FROM dual;
STR
---------
A_B_C_D_E
How it works:
REGEXP_REPLACE will replace all numeric occurrences '[0-9]+' from the string. Alternatively, you could also use POSIX character class '[^[:digit:]]+'
TRIM BOTH '_' will remove any leading and lagging _ from the string.
Also using REGEXP_SUBSTR:
SELECT trim(BOTH '_' FROM
(REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+'))) str
FROM dual;
STR
---------
A_B_C_D_E

How can I search for occurences in a string with Oracle REGEXP_INSTR

I have a string like A12345678B
I want to be able to check if a string starts with a character, is followed by 8 digits and ends with a character.
We are trying:
SELECT REGEXP_INSTR('A12345678B', '[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{10}',1,1,1,'i') from DUAL
This returns :
11
We want to be able to determine that if a string does NOT start and end with a character and is not followed by 8 digits after the first character ( see sample string above ), THEN this is not the string that we are looking for .
string starts with a character
^[[:alpha:]]
is followed by 8 digits
[[:digit:]]{8}
ends with a character
[[:alpha:]]$
So the complete regex would be,
^[[:alpha:]][[:digit:]]{8}[[:alpha:]]$
This [[:alpha:]]{10} in your regex assumes that there must be exactly 10 alphabets present after to the 8 digit number.
^[a-zA-Z][0-9]{8}[a-zA-Z]$
Try this.Put the anchors.
Try using regexp_like if you want a full string match. For a match, use:
WHERE REGEXP_LIKE('A12345678B', '^[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{1}$')
For a non-match use:
WHERE NOT REGEXP_LIKE('A12345678B', '^[[:alpha:]]{1}[[:digit:]]{8}[[:alpha:]]{1}$')
Note: this assumes that you want to filter the results (which is what your question implies, not put a flag into the select clause.

Oracle SQL: Remove specific number from from the initial part of the string

I have a alhpanumeric string. I also have one number with me. The string will always start with this number. How do I separate this number from the string and get the remaining part of the string?
e.g.
string => 21fgggg21.lkkk and number=> 21
result=> fgggg21.lkkk
or
string=> 215699898.55fff and number=> 2
result=> 15699898.55fff
Any hint would be appreciated.
Thanks.
substr(string, length(number)+1)
or
regexp_replace(string, '^'||number)
You could also use REGEXP_REPLACE. To remove '21' from the beginning of the string:
SELECT REGEXP_REPLACE('21fgggg21.lkkk', '^21') FROM DUAL;
REGEXP_REPLA
------------
fgggg21.lkkk
To remove '2' from the beginning of the string:
SELECT REGEXP_REPLACE('215699898.55fff', '^2') FROM DUAL;
REGEXP_REPLACE
--------------
15699898.55fff
By way of explanation...
The caret (^) means "anchor to the beginning of the string".
^21 means "match 21 at the beginning of the string".
REGEXP_REPLACE has an optional third parameter of what to replace the matched string with. Because you just want to remove the matched string you can omit the parameter, which replaces it with nothing.
If you are just looking to select it, you can use a combination of substr and instr.
substr(string, instr(string, 'number') + 1, len(string))
Your result should basically be the string started after where the number is located.

How to get rightmost 10 places of a string in oracle

I am trying to fetch an id from an oracle table. It's something like TN0001234567890345. What I want is to sort the values according to the right most 10 positions (e.g. 4567890345). I am using Oracle 11g. Is there any function to cut the rightmost 10 places in Oracle SQL?
You can use SUBSTR function as:
select substr('TN0001234567890345',-10) from dual;
Output:
4567890345
codaddict's solution works if your string is known to be at least as long as the length it is to be trimmed to. However, if you could have shorter strings (e.g. trimming to last 10 characters and one of the strings to trim is 'abc') this returns null which is likely not what you want.
Thus, here's the slightly modified version that will take rightmost 10 characters regardless of length as long as they are present:
select substr(colName, -least(length(colName), 10)) from tableName;
Another way of doing it though more tedious. Use the REVERSE and SUBSTR functions as indicated below:
SELECT REVERSE(SUBSTR(REVERSE('TN0001234567890345'), 1, 10)) FROM DUAL;
The first REVERSE function will return the string 5430987654321000NT.
The SUBSTR function will read our new string 5430987654321000NT from the first character to the tenth character which will return 5430987654.
The last REVERSE function will return our original string minus the first 8 characters i.e. 4567890345
SQL> SELECT SUBSTR('00000000123456789', -10) FROM DUAL;
Result: 0123456789
Yeah this is an old post, but it popped up in the list due to someone editing it for some reason and I was appalled that a regular expression solution was not included! So here's a solution using regex_substr in the order by clause just for an exercise in futility. The regex looks at the last 10 characters in the string:
with tbl(str) as (
select 'TN0001239567890345' from dual union
select 'TN0001234567890345' from dual
)
select str
from tbl
order by to_number(regexp_substr(str, '.{10}$'));
An assumption is made that the ID part of the string is at least 10 digits.