[SalesForce] How to check if a text field is not null, not empty, doesn’t contain white space in SOQL

How to check if a text field is not null, not empty, doesn't contain white space in SOQL? Other than using something like,

AND (field__c != null AND field__c != '' AND Field__c != ' ')

in the WHERE clause of my SOQL, which isn't taking all the situations into account, how can I do this? I'm looking at the string class method such as,

String.isNotBlank()

I know I can iterate over the result of the query and filter out what I don't need but I'm asking this question to avoid that if possible to implement something within the query itself. Thanks!

Best Answer

SOQL cannot do this directly, it has no ISEMPTY() function. Even if it did, using negative filters has implications with query selectivity.

The best approach here, if you want to avoid doing the work in Apex, would be to create a formula field (boolean/checkbox) that tells you whether or not a field is blank, and then including that in your query (either WHERE Formula_Field__c = true or WHERE Formula_Field__c = false)

Related Topic