Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive Query

I have some ticket numbers that may have numbers and letters mixed. I'll be passing in the ticketNumberIds into this method (user input) and I want it to query against the Oracle database and have the query pick up tickets that differ by upper and lower cases.

How do I make the following a case-insensitive query?

public List<TicketDO> getTicketDOsById(final List<String> ticketNumberIds) {

    String myQuery = "from TicketDO t where t.ticketNumberId in (:ticketNumberIds)";

    return getEntityManager().createQuery(myQuery).setParameter("ticketNumberIds", ticketNumberIds)
    .getResultList();

}
like image 733
Steve Avatar asked Feb 17 '11 01:02

Steve


1 Answers

You can use UPPER to perform case insensitive queries.

public List<TicketDO> getTicketDOsById(final List<String> ticketNumberIds) {

    String myQuery = "from TicketDO t where UPPER(t.ticketNumberId) in (:ticketNumberIds)";
    List<String> upperNumbers = new ArrayList<String>();
    for (String number : ticketNumberIds) {
        upperNumbers.add(number.toUppercase());
    }
    return getEntityManager().createQuery(myQuery).setParameter("ticketNumberIds", upperNumbers)
    .getResultList();

}
like image 174
James Avatar answered Oct 07 '22 08:10

James