title: JPA: How to use CASE in SELECT Query
body: <p>In the following query I want to select programs on the basis of the minimum and maximum age, but the point is it's not necessary that every program has minimum and maximum age, so I want to check in query if minimum or maximum age is present than check parameter 'age' that it should be lying between the minimum and maximum age</p>
  
  <p><strong>Query</strong></p>
  
  <pre><code>@Query("Select p from Program p where (CASE WHEN p.minimumAge!=null AND p.maximumAge==null THEN p.minimumAge&lt;=:age ELSE 0),(CASE WHEN p.minimumAge!=null AND p.maximumAge!=null THEN p.minimumAge&lt;=:age AND p.maximumAge&gt;=:age ELSE 0)")
  List&lt;Program&gt; programs(@Param("age")int age);
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: = near line 1, column 108 [Select p from com.ivl.MySportsAcademy.model.Program p where (CASE WHEN p.minimumAge!=null AND p.maximumAge==null THEN p.minimumAge&lt;=:age ELSE 0),(CASE WHEN p.minimumAge!=null AND p.maximumAge!=null THEN p.minimumAge&lt;=:age AND p.maximumAge&gt;=:age ELSE 0)]
      at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:91) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:288) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:115) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:76) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:302) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:240) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
      ... 48 common frames omitted
  </code></pre>