Oracle: SQL to query data where "not empty"?

I have the following query:

SELECT user_name FROM dm_user_sp;

Open in new window

I would like to add the clause to receive all user_name's with email only (not empty nor null values), how can I do this?
I tried:

SELECT user_name FROM dm_user_sp WHERE email NOT EMPTY
SELECT user_name FROM dm_user_sp WHERE email NOT NULL
SELECT user_name FROM dm_user_sp WHERE email NOT LIKE ''
José PerezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

select user_name from dm_user_sp where email not null

is sufficient.

NOT EMPTY would apply to collections, not for text values.
NOT LIKE ''  would reject all records.

The only other thing you might want to check for is a value of spaces.

If so... then

select user_name from dm_user_sp where trim(email) is not null

should be sufficient.  no need to check for null explicitly, it's implicit in the trim check

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Koen Van WielinkBusiness Intelligence SpecialistCommented:
Should be this:

Select user_name
from dm_user_sp 
where Coalesce(email,'') <> '';

Open in new window

Coalesce will return the first value of the parameters that are evaluated. So if email is null, it will return '', else it will return the email value.
Koen Van WielinkBusiness Intelligence SpecialistCommented:
sdstuber, I think your solution would still include email values which are empty, but not NULL. The question was to check for both NULL as well as empty strings.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

what are you considering "empty"  that is neither null nor consisting of only spaces?

I addressed both of those,  if there is another type of "empty"  what is it?
>>> where Coalesce(email,'') <> '';

this doesn't work

'' is null,   so coalesce ('','')  is coalesce(null,null)  which is null

by the same logic:  null <> ''   is the same as   null <> null,  which is false

To check if something is or is not null,  you must use the conditions is null  or is not null
Koen Van WielinkBusiness Intelligence SpecialistCommented:
Ok, I was not aware that Oracle treats empty strings as Null values. Most other databases I'm aware of do not do this. However, in the Oracle documentation I found this note:

Oracle Database currently treats a character value with a length of zero as null. However, this may not continue to be true in future releases, and Oracle recommends that you do not treat empty strings the same as nulls.

So if my logic doesn't currently work in Oracle, keeping this note in mind, you probably have to rewrite as:

Where email is not null
or trim(email) <> ''

Open in new window

This would ensure that if Oracle decides to no longer treat empty strings as null the query would still work.
>>> Where email is not null or trim(email) <> ''

that won't work either

if email = '    '

then it's not null,  so,   the OR condition will make the entire where clause true and return that row.

to do what you want,  you'd need AND,  not OR,  but as I noted in my first post,  it's not necessary to do two checks, in fact, it's a waste of time since the not null check is implicit in the trim(email) is not null check.
Mike EghtebasDatabase and Application DeveloperCommented:
as mentioned above
use Is Not Null
in place of Not Null
Koen Van WielinkBusiness Intelligence SpecialistCommented:
Sorry, you're right about the AND statement.

Funny how "standard" query language is still implemented so differently across platforms. Would've thought Null handling would be the same regardless. Good to know if I ever end up working on an Oracle platform.
null handling is the same in all of the major platforms as far as I know.

whether a zero-length string is null or not differs though and definitely makes a difference
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.