efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Add EF.Functions.Format for SQL Server string formattting

Open Joaoalen98 opened this issue 2 years ago • 6 comments

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: imagem_2023-12-28_204416464

Joaoalen98 avatar Dec 28 '23 23:12 Joaoalen98

@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.

roji avatar Dec 29 '23 13:12 roji

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 avatar Sep 01 '24 07:09 nikhil197

@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).

roji avatar Sep 01 '24 07:09 roji

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?

nikhil197 avatar Sep 01 '24 07:09 nikhil197

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

nikhil197 avatar Sep 01 '24 08:09 nikhil197

Yep, I think it's OK to just let SQL Server error here.

roji avatar Sep 01 '24 08:09 roji