Question

How do I perform substring with a parameter's length?

i.e.

Query query = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("SELECT * from User where substring(name, 1, :name.length()) like :name");
Was it helpful?

Solution

In SQL query name.length() would not work, however you can use following

Query query = HibernateUtil.getSessionFactory().getCurrentSession().
              createQuery("SELECT * from User where substring(name, 1, :length) like :name");

query.setParameter("length",name.length());
........
........
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top