Monday, December 21, 2015

IF operator alternative in JPQL

JPQL (and dialects as HQL) does not support IF operator/function, but the same functionality could be built with CASE construction:

CASE
    WHEN condition_1 THEN
         ...
    [WHEN condition_n THEN]
         ...
    ELSE
         ... 
END

Example:
SELECT e.name, 
  CASE 
    WHEN (e.salary >= 100000) THEN 1 
    WHEN (e.salary < 100000) THEN 2 
    ELSE 0  
  END 
FROM Employee e;

No comments:

Post a Comment