Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle treating empty string as NULL problem for a Java / JPA programmer

How do you handle this situation where Oracle stores the empty string as a null in the database ?

I would like it to be stored as an empty string as it is not as NULL, since issuing the query would be easier.

Something like this would select the empty string and non-empty string, but not the null values

select * from mytable where myfield like '%';

if i would like to select also the null values (which should be originally empty string), i would have to select like this :

select * from mytable where myfield like '%' or myfield is null;

i would love to skip doing or myfield is null all the time later in my sql statements

The current solution i have in mind is to take care of this in the application level, for example, in the entity, i initialize all my String field default value to a space, for example :

@Entity
public class MyEntity {
  private String name = " ";

  public void setName(String name) {
    if (isEmptyString(name)) {
      name = " ";
    }
  }
  ...

}

Or perhaps, i can make use of a new type still unknown to me from Oracle 11g that can keep empty string as it is without changing it to null value ?

Thank you !

like image 520
Albert Gan Avatar asked Apr 01 '11 07:04

Albert Gan


People also ask

Does Oracle treat empty string as null?

An empty string is treated as a NULL value in Oracle. and both of the INSERT commands as specified would be successful. They would create two rows one with a null value and another with an empty string '' in the DESCRIPTION column of the TEMP_TABLE .

Does Oracle support empty string?

Oracle reads empty strings as NULLs, while PostgreSQL treats them as empty. Concatenating NULL values with non-NULL characters results in that character in Oracle, but NULL in PostgreSQL.

How do you handle an empty string in Java?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

Why use null instead of empty string?

If you were to use s , it would actually have a value of null , because it holds absolute nothing. An empty string, however, is a value - it is a string of no characters. Null is essentially 'nothing' - it's the default 'value' (to use the term loosely) that Java assigns to any Object variable that was not initialized.


5 Answers

Yup, that's the way Oracle functions. Empty strings are treated as nulls.

You can of course "fix" this on application level - for example by storing " " values as you suggested - but first consider, what exactly is the difference with your "empty string" values compared to NULL values? Why do you need to treat them differently? I used to run into this dilemma, too, but usually found out that there are very few cases where I really need to tell the difference.

like image 72
Tommi Avatar answered Oct 02 '22 10:10

Tommi


No, there is no way to treat empty strings as empty strings. Oracle always treats a string of length zero as a NULL value.

like image 35
a_horse_with_no_name Avatar answered Oct 02 '22 09:10

a_horse_with_no_name


It´s not only the selection with special where condition but also the treating of Java String Objects. If you have a String a="" you can call its length method and get 0. If you have a String a=null you get a nullpointer exception when calling length. So working with an oracle db forces you to always check if your string is null before checking length :(

like image 34
Mike Avatar answered Oct 02 '22 09:10

Mike


try

create index idx_myfield on mytable(nvl(myfield,-1));

select * from mytable where nvl(myfield,-1)=-1;
like image 24
Dead Programmer Avatar answered Oct 02 '22 10:10

Dead Programmer


Its early for me, but isn't

select * from mytable where myfield like '%' or myfield is null

the same as

select * from mytable

So, Oracle simplifies your life! ;)

like image 25
tbone Avatar answered Oct 02 '22 10:10

tbone