How to obtain last string after right-most space - sql

Dataset looks like this
Column A
Column B
Apple
Apple Banana
Corn
Corn Chips
I would want "Banana" returned from the first row and "Chips" returned from the second row.
I've done (instr (Column B, ' ', -2)
This gives me the location of the space
I know I need to use a substr in conjunction with this, but for substr I need to specify the length of characters I want to retrieve right? How do I overcome the problem where the length of characters after the last space is different each time?

I know I need to use a substr in conjunction with this, but for substr I need to specify the length of characters I want to retrieve right?
No, if you do not specify the number of characters then the SUBSTR function will return the rest of the string.
So you can use:
SELECT SUBSTR(column_b, INSTR(column_b, ' ', -1) + 1) AS last_word
FROM table_name;
To get the substring after the last space.
Which, for the sample data:
CREATE TABLE table_name (Column_A, Column_B) AS
SELECT 'Apple', 'Apple Banana' FROM DUAL UNION ALL
SELECT 'Corn', 'Corn Chips' FROM DUAL;
Outputs:
LAST_WORD
Banana
Chips
fiddle

A simple option is to use regular expression, by fetching one or more occurrences + of a word \w which is anchored to the end $ of the string.
Sample data:
SQL> with test (cola, colb) as
2 (select 'apple', 'apple banana' from dual union all
3 select 'corn' , 'corn chips' from dual
4 )
Query begins here:
5 select cola, colb,
6 --
7 regexp_substr(colb, '\w+$') last_word
8 from test;
COLA COLB LAST_WORD
----- ------------ ------------
apple apple banana banana
corn corn chips chips
SQL>

Related

ORACLE SQL - REGEXP_LIKE Contains First Character As a Number and Second Character as an Alphabet

I am trying to generate a query in Oracle where i can get records that has first character in String as 3 or 4 AND second character is an alphabet. The rest can be anything else.
Something like this
SELECT COL1 FROM TABLE
WHERE REGEXP_LIKE (COL1, '3[A-Za-Z]')
OR REGEXP_LIKE (COL1, '4[A-Za-z]')
I Do get the output but for few records the data doesn't start with 3 or 4.
Meaning it selects those records who have 3 and An alphabet together anywhere in the column.
ex: 10573T2 (10573T2). I have to query records that should start with either 3 or 4 and the next character should be a letter.
Any help would be great
SQL> with test (col) as
2 (select '10573T2' from dual union all
3 select '3A1234F' from dual union all
4 select '23XXX02' from dual union all
5 select '4GABC23' from dual union all
6 select '31234FX' from dual
7 )
8 select col
9 from test
10 where regexp_like(col, '(^3|^4)[[:alpha:]]');
COL
-------
3A1234F
4GABC23
SQL>
begins ^ with 3 or | 4
and is followed by a letter [[:alpha:]]
As of your ^ doubts: that character has two roles:
[^ ... ] - Non-Matching Character List: matches any character not in list ...
^ - Beginning of Line Anchor: match the subsequent expression only when it occurs at the beginning of a line.
You need to anchor the pattern at the beginning of the string:
REGEXP_LIKE(COL1, '^[34][A-Za-z]')
Here is a db<>fiddle

Remove 2 characters in oracle sql

I have a column that contains 12 digits but user wants only to generate a 10 digits.
I tried the trim, ltrim function but nothing work. Below are the queries I tried.
ltrim('10', 'column_name')
ltrim('10', column_name)
ltrim(10, column_name)
For example I have a column that contains a 12 digit number
100000000123
100000000456
100000000789
and the expected result I want is
0000000123
0000000456
0000000789
To extract the last 10 characters of an input string, regardless of how long the string is (so this will work if some inputs have 10 characters, some 12, and some 15 characters), you could use negative starting position in substr:
substr(column_name, -10)
For example:
with
my_table(column_name) as (
select '0123401234' from dual union all
select '0001112223334' from dual union all
select '12345' from dual union all
select '012345012345' from dual
)
select column_name, substr(column_name, -10) as substr
from my_table;
COLUMN_NAME SUBSTR
------------- ----------
0123401234 0123401234
0001112223334 1112223334
12345
012345012345 2345012345
Note in particular the third example. The input has only 5 digits, so obviously you can't get a 10 digit number from it. The result is NULL (undefined).
Note also that if you use something like substr(column_name, 3) you will get just '345' in that case; most likely not the desired result.
try to use SUBSTR(column_name, 2)

Split multiple values in a cell to multiple rows - Oracle SQL

I have a table:
table1
values
------------
x=new letter
------------
a=old letter
ba=older letter
xq=newer letter
------------
xf=new apple
xt=new orange
x3=new fruit
xtt=new seed
I have to separate the values in each cell to multiple rows.
The following is the output:
table2
code description
x new letter
a old letter
ba older letter
xq newer letter
xf new apple
xt new orange
x3 new fruit
xtt new seed
How can this be achieved?
I would use regexp_replace() or regexp_substr():
select regexp_substr(str, '^[^=]+') as code,
regexp_substr(str, '[^=]+$') as value
Here is a db<>fiddle.
Note that this does not use values for the column name. That is a very bad choice for a column name because it is a SQL keyword.
try like below
SELECT NVL(SUBSTR('a=old letter', 0, INSTR('a=old letter', '=')-1), 'a=old letter')
AS col1, NVL(SUBSTR('a=old letter', INSTR('a=old letter', '=')+1), 'a=old letter')
FROM DUAL
so in you case
SELECT NVL(SUBSTR(values, 0, INSTR(values, '=')-1), values)
AS col1, NVL(SUBSTR(values, INSTR(values, '=')+1), values)
FROM table1

Number check in oracle sql

How to check in 10 digit number whether it contain 999 or 000 in the 4-6th bytes ?
I have a n idea with using INSTR but i don't know how to execute it
This is strange. If the "number" is really a string, then you can use like or substr():
where col like '___999%' or col like '___000%'
or:
where substr(col, 4, 3) in ('999', '000')
or even regular expressions.
Given the nature of your question, you can turn a number into a string and use these methods. However, if you are looking at particular digits, then the "number" should be stored as a string.
If they are actually numbers rather than strings then you could use numeric manipulation:
with t (n) as (
select 1234567890 from dual
union all select 1239997890 from dual
union all select 1230007890 from dual
union all select 1299967890 from dual
union all select 1234000890 from dual
)
select n,
mod(n, 10000000) as stage1,
mod(n, 10000000)/10000 as stage2,
trunc(mod(n, 10000000)/10000) as stage3,
case when trunc(mod(n, 10000000)/10000) in (0, 999) then 'Yes' else 'No' end as matches
from t;
N STAGE1 STAGE2 STAGE3 MATCHES
---------- ---------- ---------- ---------- -------
1234567890 4567890 456.789 456 No
1239997890 9997890 999.789 999 Yes
1230007890 7890 .789 0 Yes
1299967890 9967890 996.789 996 No
1234000890 4000890 400.089 400 No
Stage 1 effectively strips off the first three digits. Stage two almost strips off the last four digits, but leaves fractions, so stage 3 adds trunc() (you could also use floor()) to ignore those fractional parts.
The result of that is the numeric value of the 4-6th digits, and you can then test if that is 0, 999 or something else.
This is really looking at the 4th to 6th most significant digits, which is the same if the number is always 10 digits; if it might actually have different numbers of digits then you'd need to clarify what you want to see.
select
1 from dual where instr(98800054542,000,4,3)in (6) or instr(98800054542,999,4,3)in (6); let me know if this helped.

SQL How to extract numbers from a string?

I am working on a query in SQL that should be able to extract numbers on different/random lenght from the beginning of the text string.
Text string: 666 devils number is not 8888.
Text string: 12345 devils number is my PIN, that is 6666.
I want to get in a column
666
12345
Use a combination of Substr & instr
SELECT Substr (textstring, 1,instr(textstring,' ') - 1) AS Output
FROM yourtable
Result:
OUTPUT
666
12345
Use this if you have text at the beginning e.g. aa12345 devils number is my PIN, that is 6666. as it utilises the REGEXP_REPLACE function.
SELECT REGEXP_REPLACE(Substr (textstring, 1,instr(textstring,' ') - 1), '[[:alpha:]]','') AS Output
FROM yourtable
SQL Fiddle: http://sqlfiddle.com/#!4/8edc9/1/0
This version utilizes a regular expression which gives you the first number whether or not it's preceded by text and does not use the ghastly nested instr/substr calls:
SQL> with tbl(data) as (
select '666 devils number is not 8888' from dual
union
select '12345 devils number is my PIN, that is 6666' from dual
union
select 'aa12345 devils number is my PIN, that is 6666' from dual
)
select regexp_substr(data, '^\D*(\d+) ', 1, 1, null, 1) first_nbr
from tbl;
FIRST_NBR
---------------------------------------------
12345
666
12345
SQL>