I would like to use a static query that pulls out data using a LIKE
operator which includes null
values. For example, for the following data:
================================
| ID | CategoryName__c |
================================
| 01 | "category one" |
| 02 | "category two" |
| 03 | "category two" |
| 04 | (null) |
================================
I would like to be able to, in a single SOQL static query, pull out all four record regardless of their value. For example:
[SELCT ID, Name FROM ExampleTable__c WHERE CategoryName__c LIKE :magic]
Where magic
is some string. I have tried different variations of %
for this magic value, but it does not pick up null
values. The purpose of this is to be able to filter on one category, or not filter on any (depending upon the user input).
I could have an if statement
and two SOQL statements, but it would be so much nicer to just have one statement.
If I were not being restricted to static queries, I would solve my problem easily like this:
String sqlStatement = 'SELCT ID, Name FROM ExampleTable__c ';
if(singleCategoryOnly){
sqlStatement += 'CategoryName__c LIKE ' + userSelectedCategory;
}
//execute statement....
STATIC
query...STATIC
queries. What you are suggesting is not aSTATIC
query. The last part was to say that I know how to do it if I did not have to use static queries (in order to avoid people giving such an answer...).