Davide Gammone
How to filter query results using the length of a field in Salesforce

In Salesforce, with the SOQL language (Salesforce Object Query Language) is possible to run queries, in order to extract some data from the Salesforce internal database. In SOQL, the method length() does not exist (in SQL exist). To make a query, filtering the record extraction by the number of characters in a specific field, we have two possibilities:



  1. Create a formula field (e.g. FormulaCity__c) of type Number, and write inside it a simple formula witch calculates the length of the characters:

    LEN(City__c)

    After doing this, we can make the query with the new formula field:

    SELECT Id
    FROM  CustomObject__c
    WHERE FormulaCity__c = 5



  2. Otherwise, make the query using the LIKE operator:
    SELECT Id
    FROM CustomObject__c
    WHERE City__c LIKE '_____'

Both the queries extract all the records that have, in the City__c field, a string with a length of 5 characters.

This site uses cookies, including third-party requests, to offer you services in line with your preferences. By closing this banner, scrolling this page or clicking any element, you consent to the use of cookies. If you want to learn more or opt out of all or some cookies, click here