acl wrote:
This is interesting. So indeed, the SqlDateTime Struct does not allowed values before January 1, 1753:
https://docs.microsoft.com/en-us/dotnet/api/system.data.sqltypes.sqldatetime
However, I have no idea where this struct is used inside LLBLGen or ADO.NET. When declaring a parameter of an SqlCommand, its type is specified by a System.Data.SqlDbType Enum value, and this Enum does have a value SqlDbType.Date, that maps to the correct T-SQL type.
So in principle ADO.NET should support this.
Yes, that's what I said: SqlDbType does have a Date
type, but the parameters created to call the function are created from the values specified, which are DateTime
structs. This means our runtime will see the DateTime
values and create parameters with type SqlDbType.DateTime
, not SqlDbType.Date
At the moment there's no way to specify the type of a parameter in the mapping, so the system has to determine the parameter type based on the value that's passed in (in this case a DateTime value). It doesn't know anything about whether the type has to be 'Date', that's not currently available.
So it goes wrong because the value passed in is a DateTime
, the engine can only convert that to a DateTime
parameter, passes the value along and then it goes wrong inside SQL Server. It can't convert it to Date
because it doesn't know if it has to do that: Date
and DateTime
as database type both convert to the System.DateTime
.net type.
If you can, you can change the function to:
CREATE FUNCTION Difference(@dat1 as date, @dat2 as date)
RETURNS TABLE
AS
RETURN
(
SELECT DATEDIFF(YEAR, COALESCE(@dat1, N'1-1-1'), @dat2) as result
)
(not tested, but the idea is to pass in NULL for min-date instead of 1-1-1, so you get the max year diff, as that's what you're after I think)