开发者

Day of year in HQL?

开发者 https://www.devze.com 2023-02-17 14:05 出处:网络
Is there any way to extract the day of year (1-366) from a date in HQL? Is the only option to extend the dialects I need and explicitly register the functions? I see that the HSQL and DB2 dialects hav

Is there any way to extract the day of year (1-366) from a date in HQL? Is the only option to extend the dialects I need and explicitly register the functions? I see that the HSQL and DB2 dialects have dayofyear registered, but no luck with Or开发者_运维问答acle or SQL Server...

As far as I can tell, the only standard date part functions available are year(), month(), day(), etc, and unless I'm missing something these aren't sufficient to calculate DOY...


As far as I can tell, the only solution is to extend the existing dialects. This turned out to be fairly painless:

public class SQLServerDialect extends org.hibernate.dialect.SQLServerDialect {    
  public SQLServerDialect() {
    registerFunction( "dayofyear", new SQLFunctionTemplate( Hibernate.INTEGER, "datepart(dayofyear, ?1)" ) );         
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号