Add EF.Functions.Format for SQL Server string formattting
When i use Datetime.ToString( ) to format date for group registers by MM/yyyy for example, it would be nice if EF could translate this for native SQL function FORMAT.
Simple example below:
@Joaoalen98 the problem is that the .NET format strings accepted by DateTime.ToString() don't necessarily correspond to the ones that SQL Server FORMAT accepts; we've traditionally avoided adding translations where things don't correspond like that (though there have been some exceptions). A possibly better way is to add an EF.Functions.Format which corresponds exactly to SQL Server FORMAT.
Hi @roji / @ajcvickers Can I pick this one up?
Here's what I am planning:
- Add new DBFunction Extensions for Sql Server, to support all the types mentioned in the FORMAT doc for SqlServer
- Add a new method call translator for Format functions similar to
SqlServerDateDiffFunctionsTranslator - Add tests
@nikhil197 sounds good.
Since FORMAT supports both time and numeric types, I'm hesitating whether we should have just one EF.Functions overload accepting object, or different overloads for each type. We usually err towards specific overloads (the latter), but FORMAT seems general enough to warrant the former (e.g. I can imagine support for additional types being added in the future by SQL Server).
Hmm, that does makes sense. One question though, do we want to check for the type of arguments then? To make sure the type is one of the supported types and return error in case it's not or do we just let SQL Server do that for us?
Thinking more about this, I guess, we can just let SQL Server error out in case the type isn't supported, for better performance (avoid unnecessary checks) & for avoiding future changes on the EF side. But let me know your thoughts on this.
Thanks
Yep, I think it's OK to just let SQL Server error here.