views:

33

answers:

1

Hello,
In my table I have a serial number field, which is represneted by string.. It has a prefix and some numbers follow. Eg: ABC1234, ABC2345 etc. How to retrieve the largest value (max equivalent of int type) from this column. In my case it would be ABC2345. I probably could retrieve all the data,, sort it and get the same, but that would be slow.
thanks in advance..

+1  A: 

You need to utilize GORM criteria, projections in particular:

def c = MyEntity.createCriteria()

def maxNumber = c.get {
    projections {
        max("serialNumber")
    }
}

This assumes that your entity is named MyEntity and field is named serialNumber.

Vladimir Grichina