viernes, 25 de enero de 2013

Strings in JPA JPQL/Criteria Queries (LIKE, LOWER, UPPER, CONCAT, SUBSTRING, TRIM...)

Strings in JPA JPQL/Criteria Queries (LIKE, LOWER, UPPER, CONCAT, SUBSTRING, TRIM...)

Hola a todos,

me encontraba realizando unas consultas en jpa jpql y me encontré con este post interesante, se los paso.


LIKE - String Pattern Matching with Wildcards

The [NOT] LIKE operator checks if a specified string matches a specified pattern. The pattern  may include ordinary characters as well as the following wildcard characters:
  • The percent character (%) - which matches zero or more of any character.
  • The underscore character (_) - which matches any single character.
The left operand is always the string to check for a match (usually a path expression) and the right operand is always the pattern (usually a parameter or literal). For example: 
  • c.name LIKE '_r%' is TRUE for 'Brazil' and FALSE for 'Denmark'
  • c.name LIKE '%' is always TRUE (for any c.name value).
  • c.name NOT LIKE '%' is always FALSE  (for any c.name value).
    To match an actual underscore or percent character it has to be preceded by an escape character, which is also specified. For example:
    • '100%' LIKE '%\%' ESCAPE '\' is evaluated to TRUE.
    • '100' LIKE '%\%' ESCAPE '\' is evaluated to FALSE.
    In the expressions above only the first percent character (%) is a wildcard. The second (which appears after the escape character) represents a real % character.

    LENGTH - Counting Characters in a String

    The LENGTH(str) function returns the number of characters in the argument string as an int.
    For example:
      • LENGTH('United States') is evaluated to 13.
      • LENGTH('China') is evaluated to 5.

      LOCATE - Locating Substrings

      The LOCATE(str, substr [, start]) function searches a substring and returns its position.
      For example:
      • LOCATE('India', 'a') is evaluated to 5.
      • LOCATE('Japan', 'a', 3) is evaluated to 4.
      • LOCATE('Mexico', 'a') is evaluated to 0.
        Notice that positions are one-based (as in SQL) rather then zero-based (as in Java). Therefore, the position of the first character is 1. Zero (0) is returned if the substring is not found.
        The third argument (when present) specifies from which position to start the search.

        LOWER and UPPER - Changing String Case

        The LOWER(str) and UPPER(str) functions return a string after conversion to lowercase or uppercase (respectively).
        For example:
        • UPPER('Germany') is evaluated to 'GERMANY'.
        • LOWER('Germany') is evaluated to 'germany'.

          TRIM - Stripping Leading and Trailing Characters

          The TRIM([[LEADING|TRAILING|BOTH] [char] FROM] str) function returns a string after removing leading and/or trailing characters (usually space characters).
          For example:
          • TRIM(' UK ') is evaluated to 'UK'.
          • TRIM(LEADING FROM ' UK ') is evaluated to 'UK '.
          • TRIM(TRAILING FROM ' UK ') is evaluated to ' UK'.
          • TRIM(BOTH FROM ' UK ') is evaluated to 'UK'.
          By default, space characters are removed, but any other character can also be specified:
          • TRIM('A' FROM 'ARGENTINA') is evaluated to 'RGENTIN.
          • TRIM(LEADING 'A' FROM 'ARGENTINA') is evaluated to 'RGENTINA'.
          • TRIM(TRAILING 'A' FROM 'ARGENTINA') is evaluated to 'ARGENTIN'.

              CONCAT - String Concatenation

              The CONCAT(str1, str2, ...) function returns the concatenation of the specified strings.
              For example:
              • CONCAT('Serbia', ' and ', 'Montenegro') is evaluated to 'Serbia and Montenegro'.

              SUBSTRING - Getting a Portion of a String

              The SUBSTRING(str, pos [, length]) function returns a substring of a specified string.
              For example:
              • SUBSTRING('Italy', 3) is evaluated to 'aly'.
              • SUBSTRING('Italy', 3, 2) is evaluated to 'al'.
              Notice that positions are one-based (as in SQL) rather then zero based (as in Java). If length is not specified (the third optional argument), the entire string suffix, starting at the specified position, is returned.

              0 comentarios:

              Publicar un comentario