LIKE operator in Salesforce SOQL

In SOQL, the LIKE operator is supported for string fields only. So, we cannot use it against any other field data types.

Syntax:

String strSOQL = 'SELECT FieldAPIName FROM sObject WHERE FieldAPIName LIKE \'%' + searchKeyword + '%\'';

Example:

String strSOQL = 'Select Name From Account WHERE Name LIKE \'%' + searchKeyword + '%\'';

where searchKeyword is an Apex variable.

(or)

List < Contact > listCon = [ 
    SELECT Id, FirstName 
    FROM Contact 
    WHERE FirstName LIKE '%test%' 
];
System.debug( 'List contacts are ' + listCon );

Another Example:

String strAccountName = 'Infallible';
strAccountName = '%' + strAccountName + '%';
System.debug( [
    SELECT Id, Name, Industry, Type
    FROM Account
    WHERE Name LIKE : strAccountName
] );

Leave a Reply