| REGEXP_REPLACE IN A CASE STATEMENT [message #636645] |
Wed, 29 April 2015 12:05  |
 |
headrj
Messages: 4 Registered: April 2015 Location: UAB
|
Junior Member |
|
|
I have an input string with numerous formats. It might be:
NCA385Useless Text
NCA 386:Useless Text
NC345/545Some Useless Text
NCA400/500 Some Useless Text
NC 300/500Some Useless Text
NC 450/550(Some Uselss Text)
NCA 200 Useless Text
NCA 220/520:Useless Text
NC250 (Some Useless Text)
NC 300 (Some Useless Text)
I want to use this field in an ORACLE CASE STATEMENT WITHIN A VIEW (i.e. CREATE OR REPLACE VIEW...) like this:
case
when (course_no like '%') then
REGEXP_REPLACE ???
end course_id
I know enough about regular expressions to handle some of this, yet there are so many patterns to that data, that
guessing all of them is just impossible. The hoped for outcome of "course_id" from the input data above will be
in a column all its own (within the view) represented the following way:
NCA385
NCA386
NC345/545
NCA400/500
NC300/500
NC450/550
NCA200
NCA220/520
NC250
NC300
The input variable will always be (from the front of the variable) ^[A-Z]{2,4}. After that, there might (or might
not) be one or two spaces followed by 3 digits, followed (perhaps) by a forward slash and three more digits. If
there is no forward slash, the rest of the input variable is useless and need not be returned. If there is a for-
ward slash, return the slash and the three following digits. Eliminate any space between the last of the beginning
alphas and the first encountered digit. It just seems that at some point you could use the pipe | as a logical OR
to expand the pattern match. Any help you can provide me will be greatly appreciated.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: REGEXP_REPLACE IN A CASE STATEMENT [message #636653 is a reply to message #636652] |
Wed, 29 April 2015 14:10   |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:Finding them, it replaces them with nothing, since there is nothing following the ' ' in the REPLACE.
Correct.
Quote: I inserted $ in the ' ' and the spaces remained in the resultant data, I assume, because the REPLACE was looking for a $ sign AND did not find one, so the blanks remained.
Correct, replace is not regexp_replace, it does not search for a regular expression but for the string itself (here a space in my expression or a space followed by a dollar sign in your test).
SQL> select val,
2 replace(regexp_substr(val, '^[A-Z]{2,4} {0,2}\d{3}(/\d{3})?'),' ') res
3 from t
4 /
VAL RES
-------------------------------------------------- --------------------------------
NCA385Useless Text NCA385
NCA 386:Useless Text NCA386
NC345/545Some Useless Text NC345/545
NCA400/500 Some Useless Text NCA400/500
NC 300/500Some Useless Text NC300/500
NC 450/550(Some Uselss Text) NC450/550
NCA 200 Useless Text NCA200
NCA 220/520:Useless Text NCA220/520
NC250 (Some Useless Text) NC250
NC 300 (Some Useless Text) NC300
|
|
|
|
| Re: REGEXP_REPLACE IN A CASE STATEMENT [message #636654 is a reply to message #636653] |
Wed, 29 April 2015 14:12  |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
But you can also do it with regexp_replace:
SQL> select val,
2 regexp_replace(val, '^([A-Z]{2,4}) {0,2}({0,2}\d{3}(/\d{3})?).*$', '\1\2') res
3 from t
4 /
VAL RES
-------------------------------------------------- ------------------------------
NCA385Useless Text NCA385
NCA 386:Useless Text NCA386
NC345/545Some Useless Text NC345/545
NCA400/500 Some Useless Text NCA400/500
NC 300/500Some Useless Text NC300/500
NC 450/550(Some Uselss Text) NC450/550
NCA 200 Useless Text NCA200
NCA 220/520:Useless Text NCA220/520
NC250 (Some Useless Text) NC250
NC 300 (Some Useless Text) NC300
[Updated on: Wed, 29 April 2015 14:12] Report message to a moderator
|
|
|
|