tags:

views:

470

answers:

6

In SQL, How we make a check to filter all row which contain a column data is null or empty ?
For examile

Select Name,Age from MEMBERS

We need a check Name should not equal to null or empty.

A: 

nvl(Name, 'some dumb string') this will return Name if Name is not null and different of '' (oracle, don't know for others). It will be equal to 'some dumb string' if null or equal to ''.

Gilles
+4  A: 

This will work in all sane databases (wink, wink) and will return the rows for which name is not null nor empty

select name,age from members where name is not null and name <> ''
Vinko Vrsalovic
It doesn't work for Oracle (which is a bit odd about empty strings and nulls, admittedly).
Tony Andrews
+1  A: 

Select Name,Age from MEMBERS where name is null or name = ''

you can find and learn anymore from http://www.w3schools.com/sql/default.asp

Fuangwith S.
I think you mean where name is NOT null AND name != ''; don't you?
Ben
A: 

Depending on the Database being used; try Select Name, Age from Members where name IS NOT NULL

if you need to filter the otherway Select Name, Age from Members where name IS NULL

Certain RDBMS treat enpty string different from null, and you would need to add; Select Name, Age from Members where Name IS NOT NULL OR Name <> ''

Dheer
"Select Name, Age from Members where Name IS NOT NULL OR Name <> ''" would return rows where name = '' on DBMSs that treat NULL and '' differently, because for them '' IS NOT NULL!
Tony Andrews
ALL rdbms's SHOULD treat NULL as different from a zero length string because they *are* different. An empty string means you do have a string there, but it's zero length, a Null means an absence of data. This is not optional, it's in the specification of how RDBMS' work
Cruachan
Totally agree with you, Oracle is WRONG here. In its defence however, it has been around longer than the SQL standard!
Tony Andrews
+2  A: 

For DBMSs that treat '' as a value (not null), Vinko's query works:

select name,age from members where name is not null and name <> ''

For Oracle, which treats '' as a null, tha above doesn't work but this does:

select name,age from members where name is not null

I tried to think of a "DBMS-agnostic" solution, but failed - mainly due to the different concatenation operators/functions used by different DBMSs!

Tony Andrews
A: 

T-SQL

select name,age from members where COALESCE(name, '') <> ''
edosoft
What's the improvement over " name is not null and name <> '' " ?
Vinko Vrsalovic