oracle equivalent for position function in teradata - sql

Is there an equivalent function in Oracle to the POSITION function in teradata. http://www.sqlines.com/teradata/functions/position
In Teradata this function returns the index position of the first character of a matched word. For instance:
SELECT POSITION('Jose' IN 'San Jose');
-- Result: 5
I'm trying to find a similar function in Oracle.

INSTR, as documented in the documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions068.htm

Use INSTR in Oracle.
For example,
SQL> SELECT instr('San Jose','Jose',1) as "position" FROM dual;
position
----------
5
Read more about it in documentation here.
SQL>

Related

Position of alphabet in the column [duplicate]

Is there an equivalent function in Oracle to the POSITION function in teradata. http://www.sqlines.com/teradata/functions/position
In Teradata this function returns the index position of the first character of a matched word. For instance:
SELECT POSITION('Jose' IN 'San Jose');
-- Result: 5
I'm trying to find a similar function in Oracle.
INSTR, as documented in the documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions068.htm
Use INSTR in Oracle.
For example,
SQL> SELECT instr('San Jose','Jose',1) as "position" FROM dual;
position
----------
5
Read more about it in documentation here.
SQL>

Regular Expression in oracle 11g how to retrieve data from a column

I have the following data stored in the database column xyz:
a=222223333;b=433333657675457;c=77777;
a=52424252424242;d=5353535353;b=7373737373;
There is no requirement that b value should always be there but if b value is present I have to retrieve the value following b=.
I want to retrieve the value of b using regular expressions in Oracle and I am unable to do it.
Can anyone please help me find a solution for this?
I suggest using Oracle built-in function REGEXP_SUBSTR which returns a substring using regular expressions. According to the example you posted, the following should work.
SELECT REGEXP_SUBSTR(xyz, 'b=\d+;') FROM your_table
You can use regexp_substr:
select substr(regexp_substr(';' || xyz, ';b=\d+'), 4) from your_table;
Concatenation with ; is to distinguish between key-value pair with key say 'ab' and 'b'.
Use a Subexpression to Select a Substring of Your REGEXP_SUBSTR Matching Pattern
My match pattern, 'b=(\d+);', includes the parenthesis which mark this subexpression which is the last parameter of REGEXP_SUBSTR.
If you look at the 12c documentation, you will see that the third example uses a subexpression.
The escaped d just is a regular expression shorthand to indicate that we are looking for digits and the plus symbol is a quantifier indicating 1 or more digits.
SCOTT#db>WITH smple AS (
2 SELECT
3 'a=52424252424242;d=5353535353;b=7373737373;' dta
4 FROM
5 dual
6 ) SELECT
7 dta,
8 regexp_substr(a.dta,'b=(\d+);',1,1,NULL,1) subexp
9 FROM
10 smple a;
DTA subexp
---------------------------------------------------------
a=52424252424242;d=5353535353;b=7373737373; 7373737373
above solution is working in all the cases even if b contains alphanumeric

using length function in REGEXP_REPLACE() in Postgres

I am removing that last 3 characters from the string "ABC123" using regexp_replace function in Oracle using the below statement
select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;
The same result can be achieved in Postgres with the below statements,
select regexp_replace('ABC123','[123]', '','g')
select translate('ABC123','123', '');
Is there any way I can use the length function for replace as I have used in Oracle?
Why not simply use left()?
select left('ABC123', length('ABC123') - 3)
The same idea can be used in Oracle as well, but you need to use the substr() function. This should be more efficient in both databases.
You could also look into the trim functionality.
http://www.postgresqltutorial.com/postgresql-trim-function/
"select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;"
would become
select ltrim('ABC123','ABC') from dual;
resulting in 123

Return Only Number from string in oracle database

I have query which returns ILS 400,000.00, but i want it as 400000 only, pls advice.
thanks
you can use regex functions in the oracle database. The replace function was introduced in the Database Version 10.2:
-- \D does replace every non digit char
select regexp_replace('ILS 400,000.00', '\D*(\d+)[,.](\d+).*', '\1\2')
-- replace with capturing groups 1 and 2, which should be 400 and 000 in this example
from dual;

How to get part of the string that matched with regular expression in Oracle SQL

Lets say I have following string: 'product=1627;color=45;size=7' in some field of the table.
I want to query for the color and get 45.
With this query:
SELECT REGEXP_SUBSTR('product=1627;color=45;size=7', 'color\=([^;]+);?') "colorID"
FROM DUAL;
I get :
colorID
---------
color=45;
1 row selected
.
Is it possible to get part of the matched string - 45 for this example?
One way to do it is with REGEXP_REPLACE. You need to define the whole string as a regex pattern and then use just the element you want as the replace string. In this example the ColorID is the third pattern in the entire string
SELECT REGEXP_REPLACE('product=1627;color=45;size=7'
, '(.*)(color\=)([^;]+);?(.*)'
, '\3') "colorID"
FROM DUAL;
It is possible there may be less clunky regex solutions, but this one definitely works. Here's a SQL Fiddle.
Try something like this:
SELECT REGEXP_SUBSTR(REGEXP_SUBSTR('product=1627;color=45;size=7', 'color\=([^;]+);?'), '[[:digit:]]+') "colorID"
FROM DUAL;
From Oracle 11g onwards we can specify capture groups in REGEXP_SUBSTR.
SELECT REGEXP_SUBSTR('product=1627;color=45;size=7', 'color=(\d+);', 1, 1, 'i', 1) "colorID"
FROM DUAL;