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 !
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 .
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.
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.
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.
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.
No, there is no way to treat empty strings as empty strings. Oracle always treats a string of length zero as a NULL value.
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 :(
try
create index idx_myfield on mytable(nvl(myfield,-1));
select * from mytable where nvl(myfield,-1)=-1;
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! ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With