Oracle extract alpha characters from string - sql

I want to extract only alpha characters separately.
For example Output for "NIC132DA.1" should be "NIC" and "DA" separately without any numbers.
I tried following query:
select regexp_replace('NIC132DA.1','[^A-Za-z]') from dual;
I get following output: NICDA
Expected out put is "NIC" and "DA" separately.
Note: Number of alpha characters are not fixed in input string.

You can use REGEXP_SUBSTR:
select
regexp_substr('NIC132DA.1','[A-Za-z]+', 1, 1) first,
regexp_substr('NIC132DA.1','[A-Za-z]+', 1, 2) second
from dual;
It's better to use multilingual character class [:alpha:]:
select
regexp_substr('NIC132DA.1','[[:alpha:]]+', 1, 1) first,
regexp_substr('NIC132DA.1','[[:alpha:]]+', 1, 2) second
from dual;

Related

Date is not displaying correct with substr & like query

I am trying to get this out out,
but i am experiencing that the substr i am using is incorrect ,
For an example , all my columns are displaying
hdfs://asdasda/asdas/fdsfdsfd/received_files/asdasd_20191231_11122333_123456789_CO.dat
some of which has more character so in order for me to get the exact date in the column is inconsistent if i am using subsring
some will return 20191230
but some will return _2020123
How do we tackle this problem ?
i am trying to display only data , this is using sql language or hue ,
when i input my script in ,
select SUBSTR(input_file_name, LENGTH(input_file_name) - 44, 9) from th_ingestion_status limit 100
i feel my script for Like and substr statement is incorrect
I you want the first sequence of 8 digits surrounded by underscores, use regexp_extract():
select regexp_extract(filename, '_([0-9]{8})_', 1)
If you need this after the last /, then:
select regexp_extract(filename, '_([0-9]{8})_[^/]*$', 1)
Please use below query, also please mention the database you are using, so that can provide relevant query
substr(column_name, instr(column_name, '_', 1, 2) +1, 6)
Oracle Test Case:
select 'hdfs://asdasda/asdas/fdsfdsfd/received_files/asdasd_20191231_11122333_123456789_CO.dat', substr('hdfs://asdasda/asdas/fdsfdsfd/received_files/asdasd_20191231_11122333_123456789_CO.dat', instr('hdfs://asdasda/asdas/fdsfdsfd/received_files/asdasd_20191231_11122333_123456789_CO.dat', '_', 1, 2) +1, 6)
from dual;

ORA-01722: invalid number - value with two decimals

I'm trying to get the max value from a text field. All but two of the values are numbers with a single decimal. However, two of the values have something like 8.2.10. How can I pull back just the integer value? The values can go higher than 9.n, so I need to convert this field into a number so that I can get the largest value returned. So all I want to get back is the 8 from the 8.2.1.
Select cast(VERSION as int) is bombing out because of those two values with a second . in them.
You may derive by using regexp_substr with \d pattern :
with tab as
(
select regexp_substr('8.2.1', '\d', 1, 1) from dual
union all
select regexp_substr('9.0.1', '\d', 1, 1) from dual
)
select * from tab;
For Oracle you must attend the value as string for retire only the part before the dot. Ex:
SELECT NVL( SUBSTR('8.2.1',0, INSTR('8.2.1','.')-1),'8.2.1') AS SR FROM DUAL;
Check than the value is repeated 3 times in the sentence, and if the value is zero or the value didn't have decimal part then it will return the value as was set.
I had to use T-SQL rather PL/SQL, but the idea is the same:
DECLARE #s VARCHAR(10);
SELECT #s='8.2.1';
SELECT CAST(LEFT(#s, CHARINDEX('.', #s) - 1) AS INT);
returns the integer 8 - note that it won't work if there are no dots because it takes the part of the string to the left of the first dot.
If my quick look at equivalent functions was correct, then in Oracle that would end up as:
SELECT CAST(SUBSTR(VERSION, 1, INSTR(VERSION, '.') - 1) AS INT)

Removal of first characters in a string oracle sql

It may be very simple question, but I have run out of ideas.
I would like to remove first 5 characters from string.
Example string will look like:
1Y40K100R
I would like to display only digits that are after '%K' which in this case should give me result of 100R.
Please note that number after 'K' can have different amount of digits. It can be 4 digit number or 2 digit number.
Just use substr():
select substr(col, 6)
This returns all characters starting at the sixth.
There are multiple ways to return all characters after the k. If you know the string has a k, then use instr():
select substr(col, instr(col, 'K') + 1)
You can use regexp_substr
select regexp_substr('1Y40K100R', '(K)(.*)', 1, 1, 'i', 2) from dual
A way without regexp:
select substr('1Y40K100R', instr('1Y40K100R', 'K') +1) from dual
This may appear not so elegant, but it usually performs better than the regexp way.

Oracle - REGEXP_SUBSTR leading zeroes ignored issue

While execution below query I'm getting "235" instead of expected results "0"
select REGEXP_SUBSTR(000.235||'', '[^.]+', 1, 1) from dual;
Do this instead, and you'll see where the problem comes:
select 000.235||'' from dual
Result:
.235
The regexp picks up the first longest occurrence of non-period, which in this string is "235", so it's working correctly; it's the input value that is broken
Now, if you'd written it like this, it would be fine:
select REGEXP_SUBSTR('000.235', '[^.]+', 1, 1) from dual
So why the odd presentation of the numeric? What does your data in your table look like? This is unlikely to be the actual query you're running - if you need help with the true query, post it up
Oracle trim numeric values, you can fix it by adding ltrim to number:
select REGEXP_SUBSTR(ltrim(' 000.235')||'', '[^.]+', 1, 1) from dual;
result: 000 as expected

split a string on multiple delimiters

I have a sql that returns the example string below:
Input PKIND:BCMOX:10048301-
output BCMOX:10048301
I need to write code the first substring the string on - then split it on : and return the 2 & 3 item (BCMOX:10048301)
If the string format is consistent and you want to extract everything after the first : until the first occurrence of -, use a combination of substr and instr.
select substr(col, instr(col,':')+1, instr(col,'-')-instr(col,':')-1)
from yourtable
where instr(col,':') > 0 and instr(col,'-') > 0 --to get the rows which have these 2 characters
The REGEXP_SUBSTR version. Return everything between the first colon and the first hyphen.
select regexp_substr('PKIND:BCMOX:10048301-', ':(.*)-', 1, 1, NULL, 1) from dual;