I need to use REGEXP_REPLACE to do the following :
If word starts with 'ABCD' then replace first four(4) chars with 'FFFF'
else
If word starts with 'XYZ' then replace first three(3) chars with 'GGG'
How do I use REGEXP_REPLACE to do conditional replace ?
You can use case and string operations:
select (case when word like 'ABCD%'
then 'FFFF' || substr(word, 5)
when word like 'XYZ%'
then 'GGG' || substr(word, 4)
else word
end) as new_word
If it has to be REGEXP_REPLACE you'll have to combine two function calls:
REGEXP_REPLACE(
REGEXP_REPLACE(word,'^ABCD','FFFF')
,'^XYZ', 'GGG')
But I would prevere Gordon's caseapproach...
Related
I have following Oracle SQL code:
SELECT TO_NUMBER(TRIM(REGEXP_REPLACE(per_growth, '(%)(\s)')),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', ''') AS per_growth
FROM sometable;
This code supposed to look for percentage sign first then space and exclude them from the result. However, it is showing
ORA-01722: invalid number
error. I am learning sql yet and do not know exact cause. Is it something went wrong with (%)(\s)? The value in the table is 50%
You can use TRANSLATE to get rid of all instances of unwanted characters:
SELECT TO_NUMBER(
TRANSLATE(per_growth, '0% ', '0'),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', '''
) as per_growth
FROM sometable;
Note: TRANSLATE(expr, from_string, to_string) works by swapping all instances of the characters in from_string with the corresponding characters in to_string and if there are more characters in from_string than to_string then the remaining characters are removed. It is faster than using regular expressions and on a par with using REPLACE but it can handle multiple replacements at once, which REPLACE cannot.
If you did want to use the slower REGEXP_REPLACE then you can replace all whitespace characters and all percent characters, whether together or not, using:
SELECT TO_NUMBER(
REGEXP_REPLACE(per_growth, '[%[:space:]]'),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', '''
) as per_growth
FROM sometable;
Which, for the sample data:
CREATE TABLE sometable (per_growth) AS
SELECT '1%' FROM DUAL UNION ALL
SELECT '%2' FROM DUAL UNION ALL
SELECT '3 %' FROM DUAL UNION ALL
SELECT '4% ' FROM DUAL UNION ALL
SELECT '5,0%' FROM DUAL UNION ALL
SELECT '%%% 123 456 789,0123456 %' FROM DUAL;
Both output:
PER_GROWTH
1
2
3
4
5
123456789.0123456
db<>fiddle here
Did you try good, old REPLACE?
select replace(replace(per_growth, '%', ''), ' ', '') as result
from your_table
You can use
REGEXP_REPLACE(per_growth, '( )(%)')
in order to get rid of % sign and whitespace(s) together
or
TRIM(REPLACE(per_growth,'%'))
to get rid of % sign first, and then leading and trailing spaces next,
before numerical conversion.
I have a string with double quotes inside.
EG:
<cosmtio :ff "intermit"ksks>
I need the data between the ""
I have tried the regexp_substr but still couldn't get the value between double-quotes.
We could try using REGEXP_REPLACE here:
SELECT
string,
REGEXP_REPLACE(string, '.*"([^"]+)".*', '\1') AS quoted_term
FROM yourTable;
Data:
WITH yourTable AS (
SELECT '<cosmtio :ff "intermit"ksks>' AS string FROM dual
)
Demo
Another option, using REGEXP_SUBSTR:
SELECT
string,
TRIM(BOTH '"' FROM REGEXP_SUBSTR(string, '".*"'))
FROM yourTable;
But this approach requires nesting two function calls, which means it might not outperform the REGEXP_REPLACE version.
You need to use REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR('<cosmtio :ff "intermit"ksks>', '"([^"]+)"', 1, 1, NULL, 1) AS Result FROM DUAL
See the online demo.
The regex is simple: "([^"]+)" matches ", then captures any 1+ chars other than " into Group 1 and then matches ". The last argument is 1 telling Oracle REGEXP_SUBSTR to return the Group 1 values. The first (position) and the second (occurrence) arguments are default, 1. NULL means no specific options need to be passed to the regex engine.
You can try the following:
SELECT REGEXP_REPLACE('<cosmtio :ff "intermit"ksks>', '^[^"]*("([^"]*)")?.*', '\2') FROM dual
It is possible with regexp_substr as following:
Select
regexp_substr('<cosmtio :ff "intermit"ksks>', '[^"]+', 1, 2)
from dual;
Cheers!!
I have String like a123bcd-e2343fg-hij-dfgh
and I want OUTPUT e2343fg-hij-dfgh using Regular_expression in oracle.
select regexp_substr('abcd-efg-hij','-[^-]+'1) from dual;
You may apply regexp_substr with [^-]+[-^] pattern and then ltrim as :
select ltrim('a123bcd-e2343fg-hij-dfgh',
regexp_substr('a123bcd-e2343fg-hij-dfgh','[^-]+[-^]')) as output_string
from dual;
or better to call with bind variable :
select ltrim('&str', regexp_substr('&str','[^-]+[-^]')) as output_string
from dual;
where &str may be replaced with a123bcd-e2343fg-hij-dfgh after prompted.
Rextester Demo
Why regular expression, when a trivial SUBSTR + INSTR does the job nicely & quickly? True, it will look smarter, but I can't see any other benefit.
SQL> with test (col) as
2 (select 'a123bcd-e2343fg-hij-dfgh' from dual)
3 select substr(col, instr(col, '-') + 1) result
4 from test;
RESULT
----------------
e2343fg-hij-dfgh
SQL>
select substr('abcd-efg-hij',
regexp_instr('abcd-efg-hij','-[^-]+')+1,length('abcd-efg-hij'))
from dual;
try this
For the sake of argument, regexp_replace works too. This regex matches anything up to and including the first dash, and remembers the rest which it returns.
with tbl(str) as (
select 'a123bcd-e2343fg-hij-dfgh' from dual
)
select regexp_replace(str, '^.*?-(.*)', '\1')
from tbl;
Keep in mind if regexp_substr() does not find a match, it returns NULL but if regexp_replace() does not find a match it return the original string.
I have to remove all whitespaces in String '5 000 000,5' to '5000000,5'.
I tried 3 below but it did not work
select replace('5 000 000,5',' ','') from dual;
select regexp_replace('5 000 000,5', '[[:space:]]*','') from dual;
select regexp_replace('5 000 000,5', ' ','') from dual;
Or anyone know how to convert this String '5 000 000,5' to number because TO_NUMBER failed.
Thanks
Using REGEXP_REPLACE and SPACE class.
Select regexp_replace('your_value', '[[:space:]]+', '') from dual:
Using REPLACE
Select REPLACE('your_value', chr(32), '') from dual:
I think the proplem is your NLS_NUMERIC_CHARACTERS, that should work
select to_number('5 000 000,5', '9G999G999D0', 'NLS_NUMERIC_CHARACTERS = '', ''')
from dual
You can try this and remove any non-numeric chars like comma (,)
SELECT to_number(regexp_replace('5 000 000,5', '[^0-9]', '')) FROM dual;
Hi I want to append a letter C to a string if it starts with a number .
Also if it has any punctuation then replace with underscore _
Eg : 5-2-2-1 ==> C5_2_2_1
I tried ,but I am not able to replace the multiple occurrence of the punctuation. I am missing some simple thing, I cant get it.
SELECT REGEXP_REPLACE('9-1-1','^(\d)(-),'C\1_' ) FROM DUAL;
SELECT case when REGEXP_LIKE('9-1-1','^[[:digit:]]') then 'C' END
|| REGEXP_REPLACE('9-1-1', '[[:punct:]]', '_')
FROM DUAL;
[:digit:] any digit
[:punct:] punctuation symbol
if you have a lot of rows with different values then try to avoid regex:
SELECT case when substr('9-1-1',1,1) between '0' and '9' then 'C' end
|| translate('9-1-1', ',.!-', '_')
FROM DUAL;
Check here for example: Performance of regexp_replace vs translate in Oracle?
Try this:
select (case when substr(val, 1, 1) between '0' and '9' then 'C' else '' end) ||
regexp_replace(val, '([-+.,;:'"!])', '_')