简体   繁体   中英

how to compare string with sql date in grails

I have written query like this

Rule.findAll("FROM Rule WHERE client_id = ? " +
    "AND DATE_FORMAT(contract_begins,'%d-%m-%Y') <= ?", [tripStartDate])

But it is not able to compare the sql date with string tripStartDate , how to solve this problem,how to check contract_begins is less than or equal to tripStartDate .

The easiest way would be to construct a new data object from your string to pass into the query. Assuming (I may be misunderstanding) contract_begins is mapped as a date type in your Rule

Date tripStart = new SimpleDateFormat("dd-MM-yyyy").parse(tripStartDate)
Rule.findAllByClientAndContractBeginsLessThanEquals(client, tripStart)

or if you really want to keep the hql rather than dynamic finder

Date tripStart = new SimpleDateFormat("dd-MM-yyyy").parse(tripStartDate)
Rule.findAll("FROM Rule WHERE client_id = :clientId AND contract_begins <= :tripStart",
        [clientId: client.id, tripStart: tripStart])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM