Skip to main content
Interesting behavior with SOQL query.

Here is the setup.

Some_Field__c is a Text length 20.

Some_Field__c is not null

This is proven by running

SELECT Some_Field__c FROM Account  WHERE Id ='0013000000Ik1srAAB'  and Some_Field__c !=null

and it returns the row.

And

SELECT Some_Field__c FROM Account  WHERE Id ='0013000000Ik1srAAB'  and Some_Field__c = null

does not return a row.

The row is an empty string but

SELECT Some_Field__c FROM Account  WHERE Id ='0013000000Ik1srAAB'  and Some_Field__c = ''

doesnt return anything.

Also,

SELECT Some_Field__c FROM Account  WHERE Id ='0013000000Ik1srAAB'  and Some_Field__c = '%'

doesn't return anything.

This is happening on a summer 08 sandbox org.

How do I filter on the empty string?

5 réponses
  1. 17 juil. 2018, 08:27
    Hi Meta,

    1-If you have value of this field Some_Field__c and comparing to null value with ID then return nothing.

      Becoz query does not have this type of data, therefore, doesnt return anything.

    2-If you have empty value of this field and comparing to null empty string or null value with Id the query return nothing.

      query does not have this type of data therefore doesn't return anything.

      

    3-If you want to return Id, and some other field value whenever Some_Field__c field value is empty or null.

      then it returns the row. 

     

    String s ='';

    String query = ('Select Id,Some_Field__c from account where Some_Field__c=\'' + s+'\'');

    List<sObject> obj = database.query(query);

    System.debug('Your object field list--->'+obj);

    You can also use this.

    String query = ('Select Id, Some_Field__c from account where  Some_Field__c = Null');

    List<sObject> obj = database.query(query);

    System.debug('Your object field list--->'+obj);

    OR

    string query='Select Id, Some_Field__c from account where Some_Field__c = ' '\';

    List<sObject> obj = database.query(query);

    System.debug('Your object field list--->'+obj);

    Please let me know if you have any query.

    Please mark it as best Answer if you find it helpful.

    Thank You

    Ajay Dubedi
0/9000