Registering a SQL function with JPA and Hibernate

You might read articles telling you to register the SQL function by extending the Hibernate Dialect, but that’s a naive solution. Since Hibernate ORM 5.2.18 and 5.3.1, the best way to register a SQL function is to supply a MetadataBuilderContributor like this: public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor { @Override public void contribute(MetadataBuilder metadataBuilder) { metadataBuilder.applySqlFunction( … Read more

SQL date format convert? [dd.mm.yy to YYYY-MM-DD]

Since your input is a string in the form 03.09.13, I’ll assume (since today is September 3, 2013) that it’s dd.mm.yy. You can convert it to a date using STR_TO_DATE: STR_TO_DATE(myVal, ‘%d.%m.%y’) Then you can format it back to a string using DATE_FORMAT: DATE_FORMAT(STR_TO_DATE(myVal, ‘%d.%m.%y’), ‘%Y-%m-%d’) Note that the year is %y (lowercase “y”) in … Read more

SQL Query – Concatenating Results into One String [duplicate]

If you’re on SQL Server 2005 or up, you can use this FOR XML PATH & STUFF trick: DECLARE @CodeNameString varchar(100) SELECT @CodeNameString = STUFF( (SELECT ‘,’ + CodeName FROM dbo.AccountCodes ORDER BY Sort FOR XML PATH(”)), 1, 1, ”) The FOR XML PATH(”) basically concatenates your strings together into one, long XML result (something … Read more