I have the following query:

[SELECT Id, Name 
FROM Program__c 
WHERE Name LIKE :**newSearchText** AND (NOT name  like 'BR_%') 
    AND (NOT name  like 'PT_%') AND Portal_Sample_Template_Lookup__c = :tmpId 
LIMIT 2000]

And here the newSearchText is a text entered by the user.

At the same time, I want to avoid everything record that starts with 'BR_' or 'PT_' to be neglected but if the user enters

eg. 'BRMIG' it doesn't query for the record instead it considers it as 'BR_' and doesnt really considers the '_'. What workaround can I implement for making my result case-sensitive.

  • This question doesn't seem to match the summary; case sensitivity is not what is being covered here.
    – Phil W
    Commented Feb 21, 2023 at 18:30

1 Answer 1


Since you are using LIKE, be aware that both "%" and "_" are special characters in this context. This is covered in the documentation:

The % wildcard matches zero or more characters.

The _ wildcard matches exactly one character.

To avoid either character from being treated as a wildcard, escape the character. For your example change your query to something like:

  FROM Program__c
  WHERE Name LIKE :newSearchText
    AND (NOT Name LIKE 'BR\_%')
    AND (NOT Name LIKE 'PT\_%')
    AND Portal_Sample_Template_Lookup__c = :tmpId
  LIMIT 2000

Not the answer you're looking for? Browse other questions tagged .