Search code examples
querydsl

IF funtion in QueryDsl


How to write this query in queryDsl

select * from Table order by if(a = 0, b, a) desc

I struggle with the if(a = 0, b, a) part

SOLUTION

orderBy(Expressions.stringTemplate("if({0} = 0, {1}, {2})", a, b, a).desc())


Solution

  • orderBy((new CaseBuilder().when(a.eq(0)).then(b).otherwise(a)).desc()) is the preferred way to do this. Template expressions also work, but are more susceptible to query injection bugs.