month() function and year() function in postgresql through jpa2

这一生的挚爱 提交于 2019-12-11 07:01:58

问题


How to get the month and year of a postgresql date field using jpa2 criteria query or jpql?

Has anybody done this?


回答1:


Use this: FUNC('MyFunc', a, b, c)

Example: SELECT FUNC('to_char', current_date, 'month')




回答2:


A few examples, how to extract the month from a date or timestamp in PostgreSQL:

select to_char(now(), 'month');
select to_char(now(), 'MM');
select extract('month' from now());
select date_part('month', now());

Just read the manual.




回答3:


With JPA criteria API (tested only with Hibernate) :

// Create expressions that extract date parts:
Expression<Integer> year = cb.function("year", Integer.class, date);
Expression<Integer> month = cb.function("month", Integer.class, date);
Expression<Integer> day = cb.function("day", Integer.class, ts);

// Create expressions that extract time parts:
Expression<Integer> hour = cb.function("hour", Integer.class, time);
Expression<Integer> minute = cb.function("minute", Integer.class, time);
Expression<Integer> second = cb.function("second", Integer.class, ts); 

Source : http://www.objectdb.com/java/jpa/query/jpql/date#Date_and_Time_in_Criteria_Queries_

With jpql (tested only with Hibernate) :

YEAR(date)
MONTH(date)
DAY(date) 
HOUR(date)
MINUTE(date)
SECOND(date)


来源:https://stackoverflow.com/questions/7775187/month-function-and-year-function-in-postgresql-through-jpa2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!