3

I'm trying to write a query that simply selects all non-empty names. Both the following queries return no results:

 SELECT name FROM MyTable WHERE name != '';

 SELECT name FROM MyTable WHERE name = '';

For context, both of these queries do return results:

 SELECT name FROM MyTable WHERE name != 'a';

 SELECT name FROM MyTable WHERE name IS NOT NULL;

I read somewhere that the empty string is equivalent to NULL in oracle, but I still don't see why that explains this behaviour. I need to support both SQL Server and Oracle which is why I can't just rely on WHERE name IS NOT NULL

Can anyone explain what's happening here? Thanks!

6
  • Hi friend, this depends a bit on the type of your column. Oracle behaves a bit unexpectedly on varchar2 vs char columns. This other question has details: stackoverflow.com/questions/1268177/… Commented Jun 6, 2014 at 16:42
  • I don't know your app and support and how you do it, but this might be a task for and ORM? It would be interesting to see if it helped. Commented Jun 6, 2014 at 16:44
  • possible duplicate of Why does Oracle 9i treat an empty string as NULL? Commented Jun 6, 2014 at 16:45
  • 1
    To support both SQL Server and Oracle why don't you use (name!='' or name is not null)? Provided that each condition supports one database. Commented Jun 6, 2014 at 16:46
  • @user2672165 has it correct, although I would probably reverse the order to where (name is not null or name <> '') Commented Jun 6, 2014 at 17:14

2 Answers 2

6

Any comparison that involves a NULL value will always return FALSE.

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

2 Comments

I think that explains it, thanks. I also found this: docs.oracle.com/cd/B19306_01/server.102/b14200/… which says "Because null represents a lack of data, a null cannot be equal or unequal to any value or to another null" which explains why name is neither equal nor not-equal to null.
Any comparison that involves NULL returns UNKNOWN, not FALSE. However, a SELECT statement only returns a record when the WHERE clause is TRUE, which neither FALSE nor UNKNOWN is.
4

Can anyone explain what's happening here? Thanks!

From the Ask Tom archive.

A ZERO length varchar is treated as NULL.

'' is not treated as NULL.

'' when assigned to a char(1) becomes ' ' (char types are blank padded strings).

'' when assigned to a varchar2(1) becomes '' which is a zero length string and a zero length string is NULL in Oracle (it is no longer '')

5 Comments

@mason Essentially when you insert a zero length string into a Varchar2 column, it becomes a NULL in Oracle. This goes against what the ANSI standard reads, but Oracle has been doing their own thing forever.
So is '' a zero length string?
@mason Yes, if there are no characters between the quotes, then it is a zero length string
But you said in your answer that '' is not treated as NULL, but that zero length strings are NULL. Which is it?
@mason Was a cut/paste job from the Article, there are no zero length strings in Oracle varchars. You can do a quick test of that with: DECLARE A VARCHAR(2); BEGIN A := ''; IF A = '' THEN dbms_output.put_line('Empty String'); ELSE dbms_output.put_line('It''s NULL'); END IF; IF '' = '' THEN dbms_output.put_line('Empty String'); ELSE dbms_output.put_line('It''s NULL'); END IF; END;

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.