7

In SQL, I could to something like:

SELECT Id, Name, PostalCode FROM Account WHERE LEFT( PostalCode, 5 ) IN ('84117','84070'...)

How do I do something similar in SOQL? I see date functions and aggregate functions in the SOQL documentation but nothing on others.

I've tried the following but the query errors:

select Id, name from zip_code__c where name.left(5)  = '84070'

If someone can help with this, it sure would help.

Thanks in advance.

Sergej Utko
  • 22,020
  • 11
  • 59
  • 88
Robert Harper
  • 1,589
  • 3
  • 21
  • 33

2 Answers2

8

Doug B's response has the answer. I'll show a simple snippet so you don't have to follow the link if you don't want to.

String[] filter = new String[]{'84117%', '84070%'};
Zip_Code__c[] zipList = [SELECT Id, Name FROM Zip_Code__c WHERE Name LIKE :filter];
Sander de Jong
  • 4,078
  • 8
  • 61
  • 114
Robert Harper
  • 1,589
  • 3
  • 21
  • 33
3

Two options:

  1. Use the % wildcard

    Select Id, Name From zip_code__c Where Name like '84070%'
    
  2. Create a formula field

    Name_First_Five__c Formula Field:

    LEFT(Name, 5)
    

    Query:

    Select Id, Name From zip_code__c Where Name_First_Five__c = '84070'
    
Matt K
  • 3,400
  • 2
  • 24
  • 54