问题
I would like to use the yearweek()
function of QueryDSL, which is supported by QueryDSL. This function generates the query select year(table1.timestamp)*100 + week(table1.timestamp) as col_0_0_ [...]
. This does, however, not return a correct result on year endings. I've already opened a bug report for this issue.
I would rather use the built-in SQL yearweek()
function, which is directly supported by e.g. MariaDB. Is there a way of using the SQL function YEARWEEK(timestamp)
with QueryDSL? Or is it possible to use a custom SQL function with QueryDSL, e.g. given as a String?
Here is an example on how the two implementations return different results on year endings:
SELECT YEARWEEK("2018-01-01");
→ returns 201753, correct
SELECT YEAR("2018-01-01") * 100 + WEEK("2018-01-01")
→ returns 201800, incorrect
回答1:
I found the answer to the question in this StackOverflow answer. You can use a template for this problem.
I used the following template to create a number expression with the SQL yearweek function:
Expressions.numberTemplate(Integer.class, "yearweek({0}, 3)", table1.timestamp);
来源:https://stackoverflow.com/questions/50676538/not-converting-yearweek-function-with-querydsl