How can I replace brackets to hyphens within Oracle REGEXP_REPLACE function? - sql

I'm a newbie in regular expressions. I want to replace any text string symbols like (,),[,] to hyphens (for example):
SELECT REGEXP_REPLACE ('(<FIO>)', '(', '-') FROM dual;
This gives me ORA-12725 error.
Please, explain me, what's wrong?
Thanks.

To replace symbols, use the TRANSLATE function, it is less processor-intensive than regular expression functions:
SQL> SELECT translate ('(<FIO>)', '()[]', '----') replaced FROM dual;
REPLACED
--------
-<FIO>-
Regular expressions are more versatile and can do more complex things but are more expensive. In this case, replacing one character by another is done more efficiently by a specialized function. If you really want to use regular expressions, you could use REGEXP_REPLACE:
SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 0) reg FROM dual;
REG
---------
--<FIO>--
Update: If you want to replace only the first symbol, translate won't work. Instead, use:
SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 1) reg FROM dual;
REG
---------
-(<FIO>)]

Related

Oracle - How to use a function inside an escape with q

I know that I can escape strings in a statement like this:
select 'That''s a really funny ''joke''' from dual; --returns: That's a really funny 'joke'
Or like this:
select q'[That's a really funny 'joke']' from dual; --returns: That's a really funny 'joke'
Both are working fine.
Let's say I need to escape a string exactly in that select statement and I also need to use a function.
select q'[myfunction(somestringvariable)]' from dual;
Of course this only returns "myfunction(somestringvariable)"
As said, I need to escape the results of the function (can't be done inside the function, escape needs to happen in this select statement).
Can I use the function somehow inside a string escaped with "q"?
Thanks!
To double single quotes in the function result:
REPLACE(myfunction(somestringvariable), '''', '''''')
Oracle does not support template literals.
Just use:
SELECT myfunction(somestringvariable) FROM DUAL;
or, if the function does not return a string:
SELECT TO_CHAR(myfunction(somestringvariable)) FROM DUAL;
If you want to concatenate a quoted string literal and a function result then use string concatenation.
SELECT q'[That's a really funny 'joke']'
|| myfunction(somestringvariable)
FROM DUAL;

fixed number format with different lengths in Oracle

I need help with a Oracle Query
I have a query:
scenario 1: select to_char('1737388250',what format???) from dual;
expected output: 173,7388250
scenario 2: select to_char('173738825034',what format??) from dual;
expected output: 173,738825034
scenario 3: select to_char('17373882',what format??) from dual;
expected output: 173,73882
I need a query to satify all above scenarios?
Can some one help please?
It is possible to get the desired result with a customized format model given to to_char; I show one example below. However, any solution along these lines is just a hack (a solution that should work correctly in all cases, but using features of the language in ways they weren't intended to be used).
Here is one example - this will work if your "inputs" are positive integers greater than 999 (that is: at least four digits).
with
sample_data (num) as (
select 1737388250 from dual union all
select 12338 from dual
)
select num, to_char(num, rpad('fm999G', length(num) + 3, '9')) as formatted
from sample_data
;
NUM FORMATTED
---------- ------------
1737388250 173,7388250
12338 123,38
This assumes comma is the "group separator" in nls_numeric_characters; if it isn't, that can be controlled with the third argument to to_char. Note that the format modifier fm is needed so that no space is prepended to the resulting string; and the +3 in the second argument to rpad accounts for the extra characters in the format model (f, m and G).
You can try
select TO_CHAR(1737388250, '999,99999999999') from dual;
Take a look here
Your requirement is different so you can use substr and concatanation as follows:
select substr(your_number,1,3)
|| case when your_number >= 1000 then ',' end
|| substr(1737388250,4)
from dual;
Db<>fiddle
Your "number" is enclosed in single-quotes. This makes it a character string, albeit a string of only numeric characters. But a character string, nonetheless. So it makes no sense to pass a character string to TO_CHAR.
Everyone's suggestions are eliding over this and useing and using an actual number .. notice the lack of single-quotes in their code.
You say you always want a comma after the first three "numbers" (characters), which makes no sense from a numerical/mathematical sense. So just use INSTR and insert the comma:
select substr('123456789',1,3)||','||substr('123456789',4) from dual:
If the source data is actually a number, then pass it to to_char, and wrap that in substr:
select substr(to_char(123456789),1,3)||','||substr(to_char(123456789,4) from dual:

substr in Oracle from column

What is the Syntax to substr in Oracle to subtract a string
i have "123456789 #073"
I only want what after the #
substr (table.col, 17,3)
is that ok ?
Most likely the simplest (and most performant) way of doing this would be to use the base string functions:
SELECT SUBSTR(col, INSTR(col, '#') + 1)
FROM yourTable;
Demo
We could also try using REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(col, '.*#(.*)', '\1')
FROM yourTable;
The regex option would in general not perform as well as the first query. The reason for this is that invoking a regex incurs a performance overhead. You might want to consider a regex option if you expect that the string logic might change or get more complicated in the future. Otherwise, go with base string functions wherever possible.
I think the most direct method might be regexp_substr():
select regexp_substr('123456789 #073', '[^#]+$')
from dual;
The regular expression says: "get me all non-hash characters at the end of the string".
If you happen to know that there are 3 characters and really want the last three characters of the string:
select substr('123456789 #073', -3)

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

Oracle Look behind Positive

There is no example of look behind expression in Oracle Doc, so i tried using Java syntax,
This my query that supposed to get any digit after TOP
select regexp_substr('TIPTOP4152','(?<=TOP)\d+') sub from dual
But nothing to be displayed !
For the sake of argument, REGEXP_SUBSTR works too:
SQL> select regexp_substr('TIPTOP4152', 'TOP(\d+)', 1, 1, NULL, 1) nbr
from dual;
NBR
----
4152
SQL>
I'm not sure that Oracle supports lookbehind. Instead you should be able to do this pretty easily with regexp_replace
REGEXP_REPLACE('TIPTOP4152', '.*TOP(\d+)', '\1')