Search code examples
grailsgrails-orm

Using SUBSTRING in Grails criteria


Is there any possibility to add criteria to GORM criteria with SUBSTRING()? I created result filter for data from database, and it work perfectly. But I now want to add new criteria ( something like "find all rows from table where 2th, 3th, and 4th letter in string equals XYZ" ) and I expected to get all rows with this string containing it on exactly this position. Of course I have other criteria with "AND" added to this filter - it's why I don't want to use HSQL/SQL. Position to substring always be the 2th, 3th and 4th, and whole string size have 14 letters.


Solution

  • You could do this using a like restriction - as well as the more commonly seen % wildcard for any substring, like also has the underscore wildcard for any single character.

    like("someProperty", "_XYZ%")