1

I'm new to using SQL Server 2012 and the Date type in my C# applications and am curious about the conversion to DateTime. I have a DBML file representing my table and a type that has a server data type of Date and a type of DateTime. I'm trying to do a simple comparison between the current day and the value in my table:

if (adc.HolidaySchedules.FirstOrDefault(d => ((DateTime)d.HolidayDate).Date.AddDays(-1) == DateTime.Today.Date) != null)

It throws the following SqlException

The datepart millisecond is not supported by date function dateadd for data type date.

I'm curious as to why I'm getting this particular exception when I'm only accessing the Date and what I should be doing, explicitly or otherwise, to handle this type of conversion. I would have thought I could call AddDays() on a Date.

1
  • 1
    i would suggest to use SqlMethods to perform operation on server : SqlMethods.DateDiffYear(o.ModifiedDate, DateTime.Now) > 1 Commented Dec 19, 2013 at 13:47

1 Answer 1

2

Why are you transforming each value in column HolidayDate instead of passing the correct parameter?

var date = DateTime.Today.AddDays(1);
var item = adc.HolidaySchedules.FirstOrDefault(d => d.HolidayDate == date);
if(item != null)
{
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this does work. I'm more curious as to why it doesn't work on the HolidayDate value though if anyone has anything.
Well, you can fire SqlServer Profiler to see the actual sql query sent to the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.