Andrei Pall

Linux Software Engineering

JPQL functions cheat sheet

Here’s a concise JPQL functions cheat sheet with examples you can use directly in your queries:


1. String Functions

FunctionDescriptionExample
CONCAT(str1, str2, ...)Concatenates stringsSELECT CONCAT(e.firstName, ' ', e.lastName) FROM Employee e
SUBSTRING(str, start, length)Extracts substringSUBSTRING(e.name, 1, 3)
TRIM(str)Removes leading/trailing spacesTRIM(e.name)
LOWER(str)Converts to lowercaseLOWER(e.name)
UPPER(str)Converts to uppercaseUPPER(e.name)
LENGTH(str)Returns length of stringLENGTH(e.name)
LOCATE(substr, str)Position of substringLOCATE('Smith', e.name)

2. Numeric / Arithmetic Functions

FunctionDescriptionExample
ABS(number)Absolute valueABS(e.salary)
SQRT(number)Square rootSQRT(e.salary)
MOD(number, divisor)ModulusMOD(e.salary, 1000)
SIZE(collection)Number of elements in a collectionSIZE(e.projects)

3. Date / Time Functions

FunctionDescriptionExample
CURRENT_DATECurrent dateWHERE e.hireDate = CURRENT_DATE
CURRENT_TIMECurrent timeCURRENT_TIME
CURRENT_TIMESTAMPCurrent date and timeCURRENT_TIMESTAMP

4. Aggregate Functions

FunctionDescriptionExample
COUNT(expr)Number of elementsCOUNT(e)
SUM(expr)Sum of numeric fieldSUM(e.salary)
AVG(expr)Average valueAVG(e.salary)
MIN(expr)Minimum valueMIN(e.salary)
MAX(expr)Maximum valueMAX(e.salary)

5. Conditional / Boolean Functions

  • CASE WHEN ... THEN ... ELSE ... END for conditional expressions.

sql
SELECT e.name, CASE WHEN e.salary > 5000 THEN 'High' ELSE 'Low' END FROM Employee e

6. Using Database-Specific Functions

If your JPA provider supports FUNCTION:

java
SELECT FUNCTION('DATE_FORMAT', e.hireDate, '%Y-%m-%d') FROM Employee e SELECT FUNCTION('NOW') FROM Employee e

This lets you access functions not natively supported in JPQL (like window functions, GROUP_CONCAT, etc.).

Newer >>