function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

SOQL query to get accounts where the name does not start with A-D

Hello, How would I write the SOQL query for the following . I want to get all person accounts where the first name does NOT start with letters A through D? 
Best Answer chosen by Michael M
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

My Investigation is showing that unfortunately, it is not possible to do a whole range of characters at once

I suggest reviewing a similar question that was asked here

As a workaround, You can try with > and < and include it in your query. See the following working example
SELECT Field__c FROM MyObject__c WHERE Name >= 'aa' AND Name < 'dd'

If you find this information helpful, please mark this answer as Best. Thank You!

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

It should be something like this. Let me know if it gives you any result
Select Id, Name FROM Account WHERE IsPersonAccount=true AND
(NOT Name like '%A%') OR (NOT Name like '%D%')


// Try this in an anonymous window

Set<String> excludeletterA = new Set<String>{'%A%'};
Set<String> excludeletterD = new Set<String>{'%D%'};

List<Account> AccountList = [
    SELECT Id, Name, FROM Account  
    WHERE  isPersonAccount=true AND  (
        (NOT Name LIKE : excludeletterA) 
        OR (NOT Name LIKE: excludeletterD)
    )
];
system.debug(AccountList);

 
Michael MMichael M
Thanks so much Anudeep. Is there any way to do a whole range of letters, instead of doing each letter individually?
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

My Investigation is showing that unfortunately, it is not possible to do a whole range of characters at once

I suggest reviewing a similar question that was asked here

As a workaround, You can try with > and < and include it in your query. See the following working example
SELECT Field__c FROM MyObject__c WHERE Name >= 'aa' AND Name < 'dd'

If you find this information helpful, please mark this answer as Best. Thank You!

Anudeep
This was selected as the best answer
Michael MMichael M
Perfect- thanks very much