0

I've been trying the following but it does not return any (null) values. How can I include null values in using a regular expression with oracle an in fact return everything including null. (It is necessary to use a regex as the value is replaced with a variable in python and so the value will not always be null. Otherwise i could just leave out the expression.)

SELECT * from table WHERE
REGEXP_LIKE (column1,'.*|NULL$')
4
  • 2
    NULL is not a string. Regex can only match a string pattern. Commented Apr 8, 2016 at 11:34
  • so what would be the most elegant way to do it if I wanted to include null? Commented Apr 8, 2016 at 11:37
  • You sem to be looking for null, or any (not-null) value, which makes the check a bit pointless? Or are you saying the regex pattern is coming from a variable, so you won't really just have .*? If so your example pattern is a bit confusing. As is the working you've used. Commented Apr 8, 2016 at 11:41
  • exactly. sometimes there is a different value Commented Apr 8, 2016 at 11:42

1 Answer 1

1

Two methods.

Explicit comparison:

WHERE (REGEXP_LIKE(column1, '.*') OR column1 IS NULL)

Or replacement:

WHERE REGEXP_LIKE(COALESCE(column1, '<NULL>'), '.*|<NULL>')

The second method is more dangerous, because you could already have the replacement string in the column.

Sign up to request clarification or add additional context in comments.

4 Comments

The second method can be made safe by changing the alternative in the regexp from <NULL> to ^<NULL>$
With that said, the first method is both easier to read and understand and more efficient (no call to COALESCE on every row, and no additional string comparison for NULL values).
@mathguy - you could still get a false match if the column value is actually '<NULL>'; the anchors only stop it matching if that's part of a longer value?
Err... you are correct Sir. Another idea for saving it, just changing the alternative to ^$ (and not wrapping the column in COALESCE), doesn't work either, although based on Oracle's choice to make zero length strings be the same as NULL, it SHOULD work. It just doesn't. So, the first method it is!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.