سؤال

I want to get records from table where column value is not empty.

Assume the following data points

**my_table (table_name)**

**my_column(column_name)**
abc
def
null

xyz

So I want to find records which are not empty or null. Basically my output has to be

abc
def
xyz

What I tried is

select * from my_table where my_column!=null & my_column!='';

This is returning 0 records

So I tried searching around and found Oracle treats blank string as null and used the following query

select * from my_table where trim(my_column) is not null;

This returned me all the results except record having null value.

I want even the empty string to disappear from the results. I tried all the possible combinations from above but am not getting desired results.

هل كانت مفيدة؟

المحلول

Assuming my_column is a VARCHAR2, it sounds like you just want

SELECT *
  FROM my_table
 WHERE my_column IS NOT NULL

You'd never want to check whether my_column = null or my_column != null. Regardless of the value of my_column, those predicates will always evaluate to false. No value is ever equal to NULL and no value is ever unequal to NULL, not even another NULL value. Oracle does not have the concept of an empty string. You'd either have a NULL or you'd have a string with one or more spaces or some other filler character. So there is no need to check whether the value is empty, just check whether it is NULL.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top