How to fetch 4 characters from a string in oracle? - sql

By using following query i am able to fetch a particular string.Now this output string contains alphanumeric characters.e.g.abcd123,pqrs542356.
Now i want to fetch only first 4 characters of this string which will be always alpha bates.
Query::
(SELECT SUBSTR(in_msg, INSTR( in_msg,'?', 1, 10 )+ 1, INSTR(in_msg,'?', 1, 11 ) - INSTR( in_msg,'?', 1, 10 )- 1)
FROM emp_message
WHERE emp_no IN (SELECT emp_no
FROM main_table
WHERE name like '%abcd%')
This query returns output as e.g.abcd1234,pqrs145423.
Again i want to fetch only first 4 characters from this query output.
Can somebody help me in this.

You can use substr (like you already do):
SUBSTR(value, 1, 4)

Here it is:
SUBSTR(field, 0, 4)

select substr(x, 1, 4) from
( select x from ..... )

Related

selected splitted value in oracle

I have two columns A, B in oracle where A value has values like that xx-target-xx
xx any data but target is exists
A
--------
xx-target-xx
xx-target
i neet to return only 'target' from text
i tired this
select TRIM(substr(A, 0, instr(A, '-') - 1)) from mytable
but the result returns xx not target
Use REGEXP_SUBSTR. You want the second string of any characters except the minus sign:
select a, regexp_substr(a, '[^-]+', 1, 2) from mytable;
Using INSTR and SUBSTR instead is a tad more complicated, but possible of course:
select a, substr(a,
instr(a, '-') + 1,
instr(a || '-', '-', 1, 2) - instr(a, '-') - 1
) as value
from mytable;
Demo: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=e75b878bbd6300e9207cd698bb3029ec

How do I get substring after a character when the occurance of the character keeps changing

Example
123\.456.578.910.ABC
123\.456.578.910
Expected result
123\.456.578
123\.456.578
For the both the inputs I should get only the first 3
I tried the regexp and substring and instr but I’m not getting the results
We can use REGEXP_SUBSTR here with a capture group:
SELECT REGEXP_SUBSTR(col, '^(\d+(\.\d+)*)', 1, 1, NULL, 1)
FROM yourTable;
Demo
Traditional, substr + instr combination is another option:
Sample data:
SQL> with test (col) as
2 (select '123\.456.578.910.ABC' from dual union all
3 select '123\.456.578.910' from dual
4 )
Query begins here:
5 select col,
6 substr(col, 1, instr(col, '.', 1, 3) - 1) result
7 from test;
COL RESULT
-------------------- --------------------
123\.456.578.910.ABC 123\.456.578
123\.456.578.910 123\.456.578
SQL>
If you value will always have at least 3 . characters then you can use:
SELECT value,
SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1) AS expected
FROM table_name;
If it may have fewer and you want the entire string in those cases then:
SELECT value,
CASE INSTR(value, '.', 1, 3)
WHEN 0
THEN value
ELSE SUBSTR(value, 1, INSTR(value, '.', 1, 3) - 1)
END AS expected
FROM table_name;
Which, for your sample data:
CREATE TABLE table_name (value) AS
SELECT '123\.456.578.910.ABC' FROM DUAL UNION ALL
SELECT '123\.456.578.910' FROM DUAL;
Both outputs:
VALUE
EXPECTED
123.456.578.910.ABC
123.456.578
123.456.578.910
123.456.578
db<>fiddle here

REGEXP_SUBSTR SPLIT Function

i want to split this into 2019/GA/0000104
select REGEXP_SUBSTR('2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35', '[^,]+')
from dual;
Output = 2019/GA/0000104
can u guys help?
It seems that you want to extract the second substring. If that's so, then you could use
regexp_substr (result), or
substr + inenter code herestr combination (result2)
SQL> with test (col) as
2 (select '2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35' from dual)
3 select regexp_substr(col, '[^,]+', 1, 2) result,
4 --
5 substr(col, instr(col, ',', 1, 1) + 1,
6 instr(col, ',', 1, 2) - instr(col, ',', 1, 1) - 1
7 ) result2
8 from test;
RESULT RESULT2
--------------- ---------------
2019/GA/0000104 2019/GA/0000104
SQL>
Try using REGEXP_SUBSTR with a capture group:
SELECT
REGEXP_SUBSTR(input, ',(.*),', 1, 1, NULL, 1)
FROM yourTable;
Demo
This form of the regex returns the second occurrence of a string of characters that are followed by a comma or the end of the line. It returns the correct element if the first one should ever be NULL.
with tbl(str) as (
select '2019/0000015,2019/GA/0000104,2cdb376e-2966-4f24-9063-f4c6f31a6f35' from dual
)
select regexp_substr(str, '(.*?)(,|$)', 1, 2, NULL, 1)
from tbl;

how fetch specific values from String in db2 query

my table has value like this "lowValue=100,upperValue=200".
i tried substr and trim function to trim lowValue= and upperValue= text. nothing worked for me
Select contantName,constantValue from Test where contantName="test1";
-------------------------------------------------
contantName constantValue
-------------------------------------------------
test1 lowValue=100,upperValue=200
-------------------------------------------------
How to get only lowValue and highvalue in select query.
i want output to fetch only numbers from the constant value 100 and 200.
select contantName, REPLACE(REPLACE(constantValue,'lowValue=',''), 'upperValue=','') from Test where contantName="test1";
This would be a solution for the sample shown
WITH test (constantValue) AS (
SELECT 'lowValue=1000,upperValue=20000' AS constantValue FROM SYSIBM.SYSDUMMY1
)
SELECT constantValue
, substr(constantValue, posstr(constantValue, 'lowValue=') + 9 , posstr(constantValue, ',') - (posstr(constantValue, '=')+1)) AS lowvalue
, substr(constantValue, posstr(constantValue, 'upperValue=') + 11 , length(constantValue) - (posstr(constantValue, 'upperValue=')+10)) AS uppervalue
FROM test
REGEXP_SUBSTR can do it
SELECT
REGEXP_SUBSTR(value, '\blowValue=(\d+)', 1, 1, 'c', 1) as "lowValue",
REGEXP_SUBSTR(value, '\bupperValue=(\d+)', 1, 1, 'c', 1) as "upperValue"
FROM (VALUES 'lowValue=54321,upperValue=123') AS base (value)

Substring from string oracle

I have strings : 'A-20-1-1', 'A-10-10', 'A-10-11-1'
And result from substringing:
'A-20-1-1', 'A-10-10', 'A-10-11-1'
1 10 11
Code won't works fine:
Select Substr(string, instr(string,'-',1,2)+1, instr(string, '-',1,2)-1)
From dual;
At the beginning I find second '-', than next one if exists. If not I get string length.
create table a(b varchar2(20));
insert into a values('A-20-1-1');
insert into a values('A-10-10');
insert into a values('A-10-11-1');
Select
b,
substr(b,instr(b,'-',1,2)+1,decode(instr(b,'-',1,3),0,length(b)-instr(b,'-',1,2),instr(b,'-',1,3)-instr(b,'-',1,2)-1)) z
from a;
gives us what you need:
A-20-1-1 1
A-10-10 10
A-10-11-1 11
At first I find the second - sign position, and then get a substring of the value from this position to the rest of the value. Then I exclude the part of the string from previous step if the string has the - sign. Like this:
with t(d) as (
select 'A-20-1-1' from dual union all
select 'A-10-10-4' from dual union all
select 'A-10-11-1' from dual
)
select REPLACE(SUBSTR(d, INSTR(d, '-', 1, 2) + 1), SUBSTR(d, INSTR(d, '-', 1, 3))) from t
RES
---
1
10
11