SOQL Like Query?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I am trying to make a query that will search for accounts with a name similar or exactly matching a field value.



So for example I have "Sales-Force" in the field. There is an account called "Salesforce" or "Sales Force"



My current query



Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%'


This isn't returning my accounts named "Sales Force" or "Salesforce" in my query editor.



UPDATED * My Apex code



string accountName= record.Name;
if(accountName.contains('-'))

accountName = accountName.replace('-', '%');

if(accountName.contains(' '))

accountName = accountName.replace(' ', '%');

accountName= '%' + accountName+ '%';

List<Account> accountLookup = new List<Account>();
accountLookup = [Select Id, Name FROM Account WHERE Name LIKE :accountName];









share|improve this question





























    up vote
    2
    down vote

    favorite












    I am trying to make a query that will search for accounts with a name similar or exactly matching a field value.



    So for example I have "Sales-Force" in the field. There is an account called "Salesforce" or "Sales Force"



    My current query



    Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%'


    This isn't returning my accounts named "Sales Force" or "Salesforce" in my query editor.



    UPDATED * My Apex code



    string accountName= record.Name;
    if(accountName.contains('-'))

    accountName = accountName.replace('-', '%');

    if(accountName.contains(' '))

    accountName = accountName.replace(' ', '%');

    accountName= '%' + accountName+ '%';

    List<Account> accountLookup = new List<Account>();
    accountLookup = [Select Id, Name FROM Account WHERE Name LIKE :accountName];









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am trying to make a query that will search for accounts with a name similar or exactly matching a field value.



      So for example I have "Sales-Force" in the field. There is an account called "Salesforce" or "Sales Force"



      My current query



      Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%'


      This isn't returning my accounts named "Sales Force" or "Salesforce" in my query editor.



      UPDATED * My Apex code



      string accountName= record.Name;
      if(accountName.contains('-'))

      accountName = accountName.replace('-', '%');

      if(accountName.contains(' '))

      accountName = accountName.replace(' ', '%');

      accountName= '%' + accountName+ '%';

      List<Account> accountLookup = new List<Account>();
      accountLookup = [Select Id, Name FROM Account WHERE Name LIKE :accountName];









      share|improve this question















      I am trying to make a query that will search for accounts with a name similar or exactly matching a field value.



      So for example I have "Sales-Force" in the field. There is an account called "Salesforce" or "Sales Force"



      My current query



      Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%'


      This isn't returning my accounts named "Sales Force" or "Salesforce" in my query editor.



      UPDATED * My Apex code



      string accountName= record.Name;
      if(accountName.contains('-'))

      accountName = accountName.replace('-', '%');

      if(accountName.contains(' '))

      accountName = accountName.replace(' ', '%');

      accountName= '%' + accountName+ '%';

      List<Account> accountLookup = new List<Account>();
      accountLookup = [Select Id, Name FROM Account WHERE Name LIKE :accountName];






      apex soql query sosl






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 17 hours ago

























      asked 19 hours ago









      Alexander Atkinsoon

      374




      374




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Have you considered:



          Select Id, Name FROM Account WHERE Name LIKE '%Sales%Force%'


          or:



          String likeValue = '%Sales%Force%';

          Account accounts = [Select Id, Name FROM Account WHERE Name LIKE :likeValue];


          PS



          Just noticed Oleksandr had posted this and deleted it: please clarify your question.



          PPS



          You could add some code that tries to generalize the pattern e.g. replaces non-alphabetic characters with a % and puts a % between each character. Or you could consider e.g. a Metaphone approach to the matching.






          share|improve this answer






















          • I added the apex code im using to my op
            – Alexander Atkinsoon
            18 hours ago










          • Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
            – Alexander Atkinsoon
            17 hours ago










          • @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
            – Keith C
            17 hours ago










          • Thanks so much!
            – Alexander Atkinsoon
            17 hours ago

















          up vote
          4
          down vote













          You can't do it. It is not possible to use value of a field in condition expression, as a value.



          According to documentation




          fieldExpression uses the following syntax:



          fieldName comparisonOperator value






          A value used to compare with the value in fieldName. You must supply a
          value whose data type matches the field type of the specified field.
          You must supply a native value—other field names or calculations are
          not permitted
          . If quotes are required (for example, they are not for
          dates and numbers), use single quotes. Double quotes result in an
          error.




          update



          looks like it is XY problem, and I am sure desired result could be achieved by means of formula fields and other tools. Maybe you can post source of this question?






          share|improve this answer






















          • My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
            – Alexander Atkinsoon
            18 hours ago










          • I guess in short my question comes down to, is it possible to make queries typo-proof?
            – Alexander Atkinsoon
            18 hours ago










          • now this question is fully understandable and as for me @KeithC gave a nice answer
            – Oleksandr Berehovskiy
            18 hours ago

















          up vote
          3
          down vote













          First off, here's the LIKE docs




          LIKE Like Expression is true if the value in the specified fieldName
          matches the characters of the text string in the specified value. The
          LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL;
          it provides a mechanism for matching partial text strings and includes
          support for wildcards.



          The % and _ wildcards are supported for the LIKE operator.
          The % wildcard matches zero or more characters.
          The _ wildcard matches exactly one character.
          The text string in the specified value must be enclosed in single quotes.
          The LIKE operator is supported for string fields only.
          The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
          The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
          Don’t use the backslash character in a search except to escape a special character.


          For example, the following query matches Appleton, Apple, and Appl,
          but not Bap




          So for your query Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%', what you're asking for is all accounts with a name that contains 'Salesforce-Force', so that wouldn't match either accounts.



          So to capture all of your examples you'd need this query
          SELECT Id, Name FROM Account WHERE Name LIKE 'Sales_Force' OR Name LIKE 'SalesForce' which would match 'Sales-Force', 'Sales Force', 'SalesForce', or 'sAlEsFoRcE' (like is case-insensitive)






          share|improve this answer




















          • What if the name is a variable and not hardcoded?
            – Alexander Atkinsoon
            19 hours ago










          • works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
            – Ralph Callaway
            18 hours ago










          • not the array only works with bind variables
            – Ralph Callaway
            18 hours ago










          • and of course there is dynamic soql that let's you do it all dynamically
            – Ralph Callaway
            18 hours ago










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "459"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238840%2fsoql-like-query%23new-answer', 'question_page');

          );

          Post as a guest






























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          Have you considered:



          Select Id, Name FROM Account WHERE Name LIKE '%Sales%Force%'


          or:



          String likeValue = '%Sales%Force%';

          Account accounts = [Select Id, Name FROM Account WHERE Name LIKE :likeValue];


          PS



          Just noticed Oleksandr had posted this and deleted it: please clarify your question.



          PPS



          You could add some code that tries to generalize the pattern e.g. replaces non-alphabetic characters with a % and puts a % between each character. Or you could consider e.g. a Metaphone approach to the matching.






          share|improve this answer






















          • I added the apex code im using to my op
            – Alexander Atkinsoon
            18 hours ago










          • Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
            – Alexander Atkinsoon
            17 hours ago










          • @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
            – Keith C
            17 hours ago










          • Thanks so much!
            – Alexander Atkinsoon
            17 hours ago














          up vote
          4
          down vote



          accepted










          Have you considered:



          Select Id, Name FROM Account WHERE Name LIKE '%Sales%Force%'


          or:



          String likeValue = '%Sales%Force%';

          Account accounts = [Select Id, Name FROM Account WHERE Name LIKE :likeValue];


          PS



          Just noticed Oleksandr had posted this and deleted it: please clarify your question.



          PPS



          You could add some code that tries to generalize the pattern e.g. replaces non-alphabetic characters with a % and puts a % between each character. Or you could consider e.g. a Metaphone approach to the matching.






          share|improve this answer






















          • I added the apex code im using to my op
            – Alexander Atkinsoon
            18 hours ago










          • Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
            – Alexander Atkinsoon
            17 hours ago










          • @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
            – Keith C
            17 hours ago










          • Thanks so much!
            – Alexander Atkinsoon
            17 hours ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Have you considered:



          Select Id, Name FROM Account WHERE Name LIKE '%Sales%Force%'


          or:



          String likeValue = '%Sales%Force%';

          Account accounts = [Select Id, Name FROM Account WHERE Name LIKE :likeValue];


          PS



          Just noticed Oleksandr had posted this and deleted it: please clarify your question.



          PPS



          You could add some code that tries to generalize the pattern e.g. replaces non-alphabetic characters with a % and puts a % between each character. Or you could consider e.g. a Metaphone approach to the matching.






          share|improve this answer














          Have you considered:



          Select Id, Name FROM Account WHERE Name LIKE '%Sales%Force%'


          or:



          String likeValue = '%Sales%Force%';

          Account accounts = [Select Id, Name FROM Account WHERE Name LIKE :likeValue];


          PS



          Just noticed Oleksandr had posted this and deleted it: please clarify your question.



          PPS



          You could add some code that tries to generalize the pattern e.g. replaces non-alphabetic characters with a % and puts a % between each character. Or you could consider e.g. a Metaphone approach to the matching.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 18 hours ago

























          answered 18 hours ago









          Keith C

          92.8k1087197




          92.8k1087197











          • I added the apex code im using to my op
            – Alexander Atkinsoon
            18 hours ago










          • Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
            – Alexander Atkinsoon
            17 hours ago










          • @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
            – Keith C
            17 hours ago










          • Thanks so much!
            – Alexander Atkinsoon
            17 hours ago
















          • I added the apex code im using to my op
            – Alexander Atkinsoon
            18 hours ago










          • Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
            – Alexander Atkinsoon
            17 hours ago










          • @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
            – Keith C
            17 hours ago










          • Thanks so much!
            – Alexander Atkinsoon
            17 hours ago















          I added the apex code im using to my op
          – Alexander Atkinsoon
          18 hours ago




          I added the apex code im using to my op
          – Alexander Atkinsoon
          18 hours ago












          Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
          – Alexander Atkinsoon
          17 hours ago




          Hello, i've added some logic to my apex code. I have it check if contains and replace with %. It work for spaces, but no hyphens however... I added it to my OP
          – Alexander Atkinsoon
          17 hours ago












          @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
          – Keith C
          17 hours ago




          @AlexanderAtkinsoon The method returns a new string with the replaced characters (it does not modify the string) so you need something more like accountName = '%' + accountName.replace('-', '%').replace(' ', '%') + '%';.
          – Keith C
          17 hours ago












          Thanks so much!
          – Alexander Atkinsoon
          17 hours ago




          Thanks so much!
          – Alexander Atkinsoon
          17 hours ago












          up vote
          4
          down vote













          You can't do it. It is not possible to use value of a field in condition expression, as a value.



          According to documentation




          fieldExpression uses the following syntax:



          fieldName comparisonOperator value






          A value used to compare with the value in fieldName. You must supply a
          value whose data type matches the field type of the specified field.
          You must supply a native value—other field names or calculations are
          not permitted
          . If quotes are required (for example, they are not for
          dates and numbers), use single quotes. Double quotes result in an
          error.




          update



          looks like it is XY problem, and I am sure desired result could be achieved by means of formula fields and other tools. Maybe you can post source of this question?






          share|improve this answer






















          • My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
            – Alexander Atkinsoon
            18 hours ago










          • I guess in short my question comes down to, is it possible to make queries typo-proof?
            – Alexander Atkinsoon
            18 hours ago










          • now this question is fully understandable and as for me @KeithC gave a nice answer
            – Oleksandr Berehovskiy
            18 hours ago














          up vote
          4
          down vote













          You can't do it. It is not possible to use value of a field in condition expression, as a value.



          According to documentation




          fieldExpression uses the following syntax:



          fieldName comparisonOperator value






          A value used to compare with the value in fieldName. You must supply a
          value whose data type matches the field type of the specified field.
          You must supply a native value—other field names or calculations are
          not permitted
          . If quotes are required (for example, they are not for
          dates and numbers), use single quotes. Double quotes result in an
          error.




          update



          looks like it is XY problem, and I am sure desired result could be achieved by means of formula fields and other tools. Maybe you can post source of this question?






          share|improve this answer






















          • My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
            – Alexander Atkinsoon
            18 hours ago










          • I guess in short my question comes down to, is it possible to make queries typo-proof?
            – Alexander Atkinsoon
            18 hours ago










          • now this question is fully understandable and as for me @KeithC gave a nice answer
            – Oleksandr Berehovskiy
            18 hours ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          You can't do it. It is not possible to use value of a field in condition expression, as a value.



          According to documentation




          fieldExpression uses the following syntax:



          fieldName comparisonOperator value






          A value used to compare with the value in fieldName. You must supply a
          value whose data type matches the field type of the specified field.
          You must supply a native value—other field names or calculations are
          not permitted
          . If quotes are required (for example, they are not for
          dates and numbers), use single quotes. Double quotes result in an
          error.




          update



          looks like it is XY problem, and I am sure desired result could be achieved by means of formula fields and other tools. Maybe you can post source of this question?






          share|improve this answer














          You can't do it. It is not possible to use value of a field in condition expression, as a value.



          According to documentation




          fieldExpression uses the following syntax:



          fieldName comparisonOperator value






          A value used to compare with the value in fieldName. You must supply a
          value whose data type matches the field type of the specified field.
          You must supply a native value—other field names or calculations are
          not permitted
          . If quotes are required (for example, they are not for
          dates and numbers), use single quotes. Double quotes result in an
          error.




          update



          looks like it is XY problem, and I am sure desired result could be achieved by means of formula fields and other tools. Maybe you can post source of this question?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 18 hours ago

























          answered 19 hours ago









          Oleksandr Berehovskiy

          8,67031935




          8,67031935











          • My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
            – Alexander Atkinsoon
            18 hours ago










          • I guess in short my question comes down to, is it possible to make queries typo-proof?
            – Alexander Atkinsoon
            18 hours ago










          • now this question is fully understandable and as for me @KeithC gave a nice answer
            – Oleksandr Berehovskiy
            18 hours ago
















          • My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
            – Alexander Atkinsoon
            18 hours ago










          • I guess in short my question comes down to, is it possible to make queries typo-proof?
            – Alexander Atkinsoon
            18 hours ago










          • now this question is fully understandable and as for me @KeithC gave a nice answer
            – Oleksandr Berehovskiy
            18 hours ago















          My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
          – Alexander Atkinsoon
          18 hours ago




          My question comes from a trigger i've created that checks a text field and searches a record based on its value. A lookup field is filled with the ID of the record found. I've been asked to make sure it checks for variations. Incase someone puts a hyphen in the name instead of a space for example it will still find the record.
          – Alexander Atkinsoon
          18 hours ago












          I guess in short my question comes down to, is it possible to make queries typo-proof?
          – Alexander Atkinsoon
          18 hours ago




          I guess in short my question comes down to, is it possible to make queries typo-proof?
          – Alexander Atkinsoon
          18 hours ago












          now this question is fully understandable and as for me @KeithC gave a nice answer
          – Oleksandr Berehovskiy
          18 hours ago




          now this question is fully understandable and as for me @KeithC gave a nice answer
          – Oleksandr Berehovskiy
          18 hours ago










          up vote
          3
          down vote













          First off, here's the LIKE docs




          LIKE Like Expression is true if the value in the specified fieldName
          matches the characters of the text string in the specified value. The
          LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL;
          it provides a mechanism for matching partial text strings and includes
          support for wildcards.



          The % and _ wildcards are supported for the LIKE operator.
          The % wildcard matches zero or more characters.
          The _ wildcard matches exactly one character.
          The text string in the specified value must be enclosed in single quotes.
          The LIKE operator is supported for string fields only.
          The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
          The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
          Don’t use the backslash character in a search except to escape a special character.


          For example, the following query matches Appleton, Apple, and Appl,
          but not Bap




          So for your query Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%', what you're asking for is all accounts with a name that contains 'Salesforce-Force', so that wouldn't match either accounts.



          So to capture all of your examples you'd need this query
          SELECT Id, Name FROM Account WHERE Name LIKE 'Sales_Force' OR Name LIKE 'SalesForce' which would match 'Sales-Force', 'Sales Force', 'SalesForce', or 'sAlEsFoRcE' (like is case-insensitive)






          share|improve this answer




















          • What if the name is a variable and not hardcoded?
            – Alexander Atkinsoon
            19 hours ago










          • works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
            – Ralph Callaway
            18 hours ago










          • not the array only works with bind variables
            – Ralph Callaway
            18 hours ago










          • and of course there is dynamic soql that let's you do it all dynamically
            – Ralph Callaway
            18 hours ago














          up vote
          3
          down vote













          First off, here's the LIKE docs




          LIKE Like Expression is true if the value in the specified fieldName
          matches the characters of the text string in the specified value. The
          LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL;
          it provides a mechanism for matching partial text strings and includes
          support for wildcards.



          The % and _ wildcards are supported for the LIKE operator.
          The % wildcard matches zero or more characters.
          The _ wildcard matches exactly one character.
          The text string in the specified value must be enclosed in single quotes.
          The LIKE operator is supported for string fields only.
          The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
          The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
          Don’t use the backslash character in a search except to escape a special character.


          For example, the following query matches Appleton, Apple, and Appl,
          but not Bap




          So for your query Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%', what you're asking for is all accounts with a name that contains 'Salesforce-Force', so that wouldn't match either accounts.



          So to capture all of your examples you'd need this query
          SELECT Id, Name FROM Account WHERE Name LIKE 'Sales_Force' OR Name LIKE 'SalesForce' which would match 'Sales-Force', 'Sales Force', 'SalesForce', or 'sAlEsFoRcE' (like is case-insensitive)






          share|improve this answer




















          • What if the name is a variable and not hardcoded?
            – Alexander Atkinsoon
            19 hours ago










          • works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
            – Ralph Callaway
            18 hours ago










          • not the array only works with bind variables
            – Ralph Callaway
            18 hours ago










          • and of course there is dynamic soql that let's you do it all dynamically
            – Ralph Callaway
            18 hours ago












          up vote
          3
          down vote










          up vote
          3
          down vote









          First off, here's the LIKE docs




          LIKE Like Expression is true if the value in the specified fieldName
          matches the characters of the text string in the specified value. The
          LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL;
          it provides a mechanism for matching partial text strings and includes
          support for wildcards.



          The % and _ wildcards are supported for the LIKE operator.
          The % wildcard matches zero or more characters.
          The _ wildcard matches exactly one character.
          The text string in the specified value must be enclosed in single quotes.
          The LIKE operator is supported for string fields only.
          The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
          The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
          Don’t use the backslash character in a search except to escape a special character.


          For example, the following query matches Appleton, Apple, and Appl,
          but not Bap




          So for your query Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%', what you're asking for is all accounts with a name that contains 'Salesforce-Force', so that wouldn't match either accounts.



          So to capture all of your examples you'd need this query
          SELECT Id, Name FROM Account WHERE Name LIKE 'Sales_Force' OR Name LIKE 'SalesForce' which would match 'Sales-Force', 'Sales Force', 'SalesForce', or 'sAlEsFoRcE' (like is case-insensitive)






          share|improve this answer












          First off, here's the LIKE docs




          LIKE Like Expression is true if the value in the specified fieldName
          matches the characters of the text string in the specified value. The
          LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL;
          it provides a mechanism for matching partial text strings and includes
          support for wildcards.



          The % and _ wildcards are supported for the LIKE operator.
          The % wildcard matches zero or more characters.
          The _ wildcard matches exactly one character.
          The text string in the specified value must be enclosed in single quotes.
          The LIKE operator is supported for string fields only.
          The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
          The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
          Don’t use the backslash character in a search except to escape a special character.


          For example, the following query matches Appleton, Apple, and Appl,
          but not Bap




          So for your query Select Id, Name FROM Account WHERE Name LIKE '%Sales-Force%', what you're asking for is all accounts with a name that contains 'Salesforce-Force', so that wouldn't match either accounts.



          So to capture all of your examples you'd need this query
          SELECT Id, Name FROM Account WHERE Name LIKE 'Sales_Force' OR Name LIKE 'SalesForce' which would match 'Sales-Force', 'Sales Force', 'SalesForce', or 'sAlEsFoRcE' (like is case-insensitive)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 19 hours ago









          Ralph Callaway

          16.3k1172151




          16.3k1172151











          • What if the name is a variable and not hardcoded?
            – Alexander Atkinsoon
            19 hours ago










          • works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
            – Ralph Callaway
            18 hours ago










          • not the array only works with bind variables
            – Ralph Callaway
            18 hours ago










          • and of course there is dynamic soql that let's you do it all dynamically
            – Ralph Callaway
            18 hours ago
















          • What if the name is a variable and not hardcoded?
            – Alexander Atkinsoon
            19 hours ago










          • works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
            – Ralph Callaway
            18 hours ago










          • not the array only works with bind variables
            – Ralph Callaway
            18 hours ago










          • and of course there is dynamic soql that let's you do it all dynamically
            – Ralph Callaway
            18 hours ago















          What if the name is a variable and not hardcoded?
          – Alexander Atkinsoon
          19 hours ago




          What if the name is a variable and not hardcoded?
          – Alexander Atkinsoon
          19 hours ago












          works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
          – Ralph Callaway
          18 hours ago




          works just fine, you can actually pass in a array of stuff ie. LIKE :values where values is array with ('val1%', '%val1','other%vals')
          – Ralph Callaway
          18 hours ago












          not the array only works with bind variables
          – Ralph Callaway
          18 hours ago




          not the array only works with bind variables
          – Ralph Callaway
          18 hours ago












          and of course there is dynamic soql that let's you do it all dynamically
          – Ralph Callaway
          18 hours ago




          and of course there is dynamic soql that let's you do it all dynamically
          – Ralph Callaway
          18 hours ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f238840%2fsoql-like-query%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo