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

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.

Related

replace all occurrences of a sub string between 2 charcters using sql

Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.

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 to return the second to last character in a string using SQL

I am trying to only return the second to last character from a string using MS SQL.
I've tried using MID and Substring but the length of the string isn't always the same for the column I am trying to return, So I can't do it that way.
So say I am returning the codes of something:
Code
'1234'
I want to just return '3' from that code.
How can I do this?
Cheers in advance :)
Use SUBSTRING and LEN. LEN gives you the length of the string, then subtract 1 to get the previous char:
SELECT SUBSTRING(Code, LEN(Code)-1,1)
How about
select left(right(code, 2), 1) from MyTable;
You might need to validate that the string actually has at least 2 chars, however.
SqlFiddle here

Oracle: remove first 4 characters from a string

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;

Select vowels from a varchar, Oracle PL/SQL

I'm trying to pull up the count of the vowels contained in a varchar,
I've been looking around in google, no success though.
Can anyone give me a hand with this one?
Something like
select length(regexp_replace('andrew','[^AEIOUaeiou]')) as vowels from dual;
If you're using Oracle 11g, you can use the REXEXP_COUNT function to determine what matches the pattern.
SQL> select regexp_count('andrew', '[aeiou]', 1, 'i') as vowels
2 from dual;
VOWELS
----------
2
The first parameter is the string you want to match, 'andrew'.
The second parameter is the match pattern, in this case [aeiou]. The [] indicates a character list; the parser matches any and all characters inside this list in any order.
The third parameter, 1, is the start position indicating the positional index of the string where Oracle should start searching for a match. It's included solely so I can use the fourth parameter.
The fourth parameter is a match parameter, 'i' indicates that I want to do case insensitive matching. This is the reason why the character list is not [aeiouAEIOU].
If you're using 10g then REGEXP_COUNT doesn't exist. In this case you could use a more exact version of Annjawan's solution with REGEXP_REPLACE.
SQL> select length(regexp_replace('andrew','[^aeiou]', '', 1, 0, 'i')) as vowels
2 from dual;
VOWELS
----------
2
The carat (^) indicates a not, i.e. the replaces every character in the string 'andrew' that is not in the character list [aeiou] with the empty string. The next parameter, once again, is the start position. The fifth parameter, 0 indicates that you want to replace every occurrence of the pattern that matches and once again I've used the match parameter 'i' to indicate case insensitive matching.
Gaurav's answer is incorrect. This is because within the character list he has included comma's. Remember that everything within the character list get's matched if it is available. So, if I introduce a comma into your string you'll have 3 "vowels" in your string:
SQL> select regexp_count('an,drew','[a,e,i,o,u,A,E,I,O,U]' ) as vowels
2 from dual;
VOWELS
----------
3
Regular expressions are not simple beasts and I would highly recommend reading the documentation when attempting them.
SELECT length('andrew')
- length(REGEXP_REPLACE('andrew','[a,e,i,o,u,A,E,I,O,U]',''))
FROM DUAL;
Output:2 -- a and e are two vowels here.
If you are using Oracle 11g then
SELECT REGEXP_COUNT('andrew','[a,e,i,o,u,A,E,I,O,U]' ) from dual