November 29, 2010

XPath Query - Perform a case insensitive comparison

To perform a case insensitive comparison you may use the function : CompareCaseInsensitive.

Example:
/sitecore/content//*[CompareCaseInsensitive(@@myfield,'MyValue')]


If you need to compare the field 'name' case insensitively you may use 'key' in place of 'name'.
Example:
/sitecore/content//*[@@key,'myvalue')]


Important: the search value has to be lower case!


All the XPath query function are available in Sitecore.Data.Query.Functions

2 comments:

  1. Hey, I have tried CompareCaseInsensitive function in sitecore query, it does not work.
    /sitecore/content/Data/Doctors//*[CompareCaseInsensitive(@@FirstName,'walid')].
    here firstname is field name.

    ReplyDelete
  2. Hello,
    It is simply because you have use a @@ for a custom field so you query should be:
    /sitecore/content/Data/Doctors//*[CompareCaseInsensitive(@FirstName,'walid')
    The @@ is only for the system fields like the name, the key, ...
    Regards,

    ReplyDelete