I have a database that stores dates broken into int’s. I.e. Day, Month and Year are stored separately in different columns.
In existing SQL queries, the method used is:
DATEADD(dd, – 1, DATEADD(mm, Z.LastMonth, DATEADD(yy, Z.LastYear – 1900, 0))) AS last_date
I cannot change the database to store dates instead.
Looking further into the dateadd() function, it’s converting from an integer (0).
Linq to SQL does not have a similar functionality i.e. Convert.ToDateTime(0). That results in an InvalidCastException.
I have tried concatenating strings to create a date, but it is WAYYYYY tooooo sllloowww. the time difference was about 10 minutes.
What else can I do? I don’t particularly want to start mixing in SQL queries into the project either.
Thanks.
Can you not just write:
Alright, I tested this in Linqpad and it seems that Linq to SQL does decide to generate some weird character-conversion query. I’m not entirely convinced that this is the source of the perf issue, but you can force a projection like this:
This will also get you the last day of the next month.
If you’re really seeing such a huge performance, difference, though, I would venture a guess that you’re looking in the wrong place, and that there’s something else that’s different. 90% of all database-related performance problems are due to poor or nonexistent indexing or non-sargable queries.