I have a table with a "Link" attribute. It has the following meaning:
INC102
INC1020
INC10200
I want to get the following result:
INC102
INC1020
INC10200
I need to leave the INC and the numbers after it without .
Tell me which command will help here? Since I understand that "Substr" will not work here.
I am use SQL Developer - Oracle
If you just want to extract "INC" with the following digits, use regexp_substr():
select regexp_substr(link, 'INC[0-9]+')
Here is a db<>fiddle.
Since I understand that "Substr" will not work here.
Says who?
SQL> with test (col) as
2 (select 'INC102' from dual union all
3 select 'INC1020' from dual union all
4 select 'INC10200' from dual
5 )
6 select
7 substr(col,
8 instr(col, '>') + 1,
9 instr(col, '<', instr(col, '>')) - instr(col, '>') - 1
10 ) result
11 from test;
RESULT
------------------------------------------------------------------------------------------------------------------------
INC102
INC1020
INC10200
SQL>
What does it do?
lines #1 - 5: sample data
line #8: starting point of the SUBSTR function is one character after the first > sign
line #9: length (used as the 3rd parameter of the SUBSTR) is position of the first < that follows the first > minus position of the first >
And that's it ... why wouldn't it work?
You could treat [<>] as the word delimiter and take the second word:
with test (col) as
( select 'INC102' from dual union all
select 'INC1020' from dual union all
select 'INC10200' from dual
)
select regexp_substr(col,'[^<>]+', 1, 2)
from test;
REGEXP_SUBSTR(COL,'[^<>]+',1,2)
-------------------------------
INC102
INC1020
INC10200
Related
I need to replace all characters with nothing before the . character and also replace all [ and ] with nothing.
Please see examples below:
from
to
[PINWHEEL_ASSET].[MX5530]
MX5530
[PINWHEEL_TRADE].[AR5403]
AR5403
The parts before and after the . dot are variables.
with
sample_data (my_string) as (
select '[PINWHEEL_ASSET].[MX5530]' from dual
)
select rtrim(substr(my_string, instr(my_string, '.') + 2), ']') as second_part
from sample_data
;
SECOND_PART
-----------
MX5530
This assumes that the input string looks exactly like this: [first].[second], where "first" and "second" are (possibly empty) strings that do not contain periods or closing brackets.
Yet another option is to use regular expressions (see line #6).
Sample data:
SQL> with test (col) as
2 (select '[PINWHEEL_ASSET].[MX5530]' from dual union all
3 select '[PINWHEEL_TRADE].[AR5403]' from dual
4 )
Query begins here:
5 select col,
6 regexp_substr(col, '\w+', 1, 2) result
7 from test;
COL RESULT
------------------------- --------------------
[PINWHEEL_ASSET].[MX5530] MX5530
[PINWHEEL_TRADE].[AR5403] AR5403
SQL>
I'm developing a command in Oracle SQL using a table that has that type of row:
company=1&product=12588&version=1
For my command, I need the product number and version of each row, but separated by columns.
My first question: How can I read only the product number, using something different of REGEXP_SUBSTR
My second question: What is the best way to create a new column to show the version without duplicate the line?
I hope someone can help me.
If data really is that simple, regular expressions make code rather simple. Way simpler than SUBSTR + INSTR option. Why don't you want to use regex? Because it is evil, or ...?
sample data in lines #1 - 4
product: take the 2nd numeric value from the column
version: take the last numeric value from the column
SQL> with test (col) as
2 (select 'company=1&product=12588&version=1' from dual union all
3 select 'company=2&product=52361&version=4' from dual
4 )
5 select col,
6 regexp_substr(col, '\d+', 1, 2) as product,
7 regexp_substr(col, '\d+$') as version
8 from test;
COL PRODUCT VERSION
--------------------------------- ---------- ----------
company=1&product=12588&version=1 12588 1
company=2&product=52361&version=4 52361 4
SQL>
As of
What is the best way to create a new column to show the version without duplicate the line?
I have no idea what that means. "Without duplicate the line"? Which line? Which duplicate?
To answer your first question, you can use sql below by combining only the ** substr ** and ** instr ** functions.
This solution takes advantage of the four and last parameter of the instr function.
select your_column
, substr(your_column
, instr(your_column, '=', 1, 1) + 1
, instr(your_column, '&', 1, 1) - instr(your_column, '=', 1, 1) - 1
) company
, substr(your_column
, instr(your_column, '=', 1, 2) + 1
, instr(your_column, '&', 1, 2) - instr(your_column, '=', 1, 2) - 1
) product
, substr(your_column
, instr(your_column, '=', 1, 3) + 1
) version
from (
select 'company=1&product=12588&version=1' your_column from dual union all
select 'company=2&product=52361&version=4' your_column from dual
) Your_data
;
demo
But, I'm not sure I understood your second question correctly.
in my table one column contains data as below
BMS/430301420-XN/0
I need to use substr function in oracle and output to be taken as
430301420-XN
the one I used is as below
substr(buy_id,5),substr(substr(buy_id,5),instr(buy_id,'/',2))
but it is not working please help me
If you know the format of the string and you always want to start on the fifth character and remove the last two, then:
select substr(str, 5, -2)
If you just want the part between the slashes, then use regexp_substr():
select replace(regexp_substr(str, '/.*/'), '/', '')
Easiest way is a Regular Expression, find the string between the slashes but don't include them in the result:
regexp_substr(buy_id, '(?<=/).*(?=/)')
With a combination of SUBSTR and INSTR:
SQL> WITH DATA AS(
2 SELECT 'BMS/430301420-XN/0' str FROM dual UNION ALL
3 SELECT 'BMSABC/430301420-XN/0' str FROM dual UNION ALL
4 SELECT 'BMS/430301420-XN/012345' str FROM dual
5 )
6 SELECT str,
7 SUBSTR(str, instr(str, '/', 1, 1)+1, instr(str, '/', 1, 2)
8 -instr(str, '/', 1, 1)-1) new_str
9 FROM DATA;
STR NEW_STR
----------------------- -----------------------
BMS/430301420-XN/0 430301420-XN
BMSABC/430301420-XN/0 430301420-XN
BMS/430301420-XN/012345 430301420-XN
SQL>
The above uses the logic to find the substring between the first and second occurrence of the /.
This will also Work :D
select Column_Name as OLD , substr(''||to_char(Column_Name)||'',instr
(''||to_char(Column_Name)||'','/',1)+1,(instr(''||to_char(Column_Name)
||'','/',1,2)-instr(''||to_char(Column_Name)||'','/',1,1)-1)) as NEW from Table_Name;
Same Use Of substr and instr
my answer is :
select
substr('BMS/430301420-XN/0',
(instr('BMS/430301420-XN/0','/') +1),
(instr('BMS/430301420-XN/0','/',(instr('BMS/430301420-XN/0','/')+1))-instr('BMS/430301420-XN/0','/')-1 ))
from dual
you can see this sample :
http://www.sqlfiddle.com/#!4/9eecb7/863/0
I have a table with a varchar column with data like this:
"<tasa>
<parametros>
<parametro>
<nombre>ea</nombre>
<valor>35</valor>
</parametro>
</parametros>
<valorTasa>3.15</valorTasa>
</tasa>"
I need to be able to extract the value between the valorTasa tags, but don't know how to use the function and can't access oracle documentation.
I'm trying something like
select regexp_substr(field, '<valorTasa>[0-9]{0-3}</valorTasa') from dual;
With no results.
Any help would be greatly appreciated
More simple way would be using extractvalue function to extract the value of the node.
-- sample of data
SQL> with t1(col) as(
2 select '<tasa>
3 <parametros>
4 <parametro>
5 <nombre>ea</nombre>
6 <valor>35</valor>
7 </parametro>
8 </parametros>
9 <valorTasa>3.15</valorTasa>
10 </tasa>'
11 from dual
12 )
13 select extractvalue(xmltype(col), '/tasa/valorTasa') as res
14 from t1
15 /
RES
-------
3.15
Actually REGEXP_REPLACE will work best for this. If you put a part of the search expression in parentheses you can refer to it in the third "replace-with" parameter - the first such expression is \1, the second is \2, and so on up to \9 (you can't do more than 9).
For your requirement, try this:
SELECT REGEXP_REPLACE(myXMLCol, '^.*<valorTasa>(.*)</valorTasa>.*$', '\1') FROM myTable
^^^^ ^^
The part in the parentheses above - (.*) maps to \1. The Oracle REGEXP_REPLACE docs explain this better than I can :)
SELECT regexp_replace(
regexp_substr(field, '<valorTasa>[0-9\.]+</valorTasa>'),
'<valorTasa>([0-9\.]+)</valorTasa>',
'\1')
from dual;
For multiline XML documents, as we have here, regexp_replace routine could be used but only with correct match_parameter = mn :
with t1(col) as(
select '<tasa>
<parametros>
<parametro>
<nombre>ea</nombre>
<valor>35</valor>
</parametro>
</parametros>
<valorTasa>3.15</valorTasa>
</tasa>'
from dual
)
select
REGEXP_REPLACE(col, '^.*<valorTasa>(.*)</valorTasa>.*$', '\1', 1, 0, 'mn') as res
from t1
/
I have an integer field in a table.
I want to read the first digit of this field, then up to that digit read next digits.
For example consider this field: 355560
I read the first digit (3)
Then read 3 digits after 3 : (555)
How would I write my select query?
SELECT SUBSTR (355560, 2, SUBSTR (355560, 1, 1))
FROM DUAL;
select substr('355560', 2, substr('355560', 0, 1)) from dual
try this one
SELECT SUBSTR(355560,1,1) FROM DUAL
OUTPUT : 3
SELECT SUBSTR(355560,2,3) FROM DUAL
OUTPUT : 555
As I understand your question:
select concat(substr(35550,1,1),substr(35550,2,3)) from dual