Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace All Occurrences using Oracle SQL regexp_replace Case-insensitive

I'd love to replace a case-insensitive string for all occurrences

The given query replaces all tons to yard. However, it is case sensitive.

SELECT regexp_replace(col_name, 'tons', 'yard') FROM DUAL;

How can I write a query that replaces all occurrences regardless of letter cases. I tried this but didn't work:

SELECT regexp_replace(col_name, 'tons', 'yard', 'i') FROM DUAL;

Thanks

like image 260
Qanat Tajrediat Avatar asked May 24 '16 22:05

Qanat Tajrediat


1 Answers

Using the 'i' option is correct, but you're missing two parameters before it.

REGEXP_REPLACE(<source_string>, <pattern>,<replace_string>, <position>, <occurrence>, <match_parameter>)

For position, use 1 to start searching at the beginning. For occurrence, use 0 to replace every occurrence.

SELECT regexp_replace(col_name, 'tons', 'yard', 1, 0, 'i') FROM DUAL;

Examples

Official documentation

like image 176
Matthew G Avatar answered Sep 30 '22 21:09

Matthew G