Sql server 操作数数据类型日期对于减法运算符无效

Sql server 操作数数据类型日期对于减法运算符无效,sql-server,function,Sql Server,Function,我的表中有一个名为[LastDate]的字段,数据类型为Date。我将编写一个函数来计算[LastDate]-@PassedParameter,但是错误发生了: Operand data type date is invalid for subtract operator. 我不知道为什么 hara是一种功能: CREATE FUNCTION Salman( @Date date ) RETURNS TABLE AS RETURN ( SELECT TOP 1000 [ID]

我的表中有一个名为
[LastDate]的字段,数据类型为Date
。我将编写一个函数来计算
[LastDate]-@PassedParameter
,但是错误发生了:

Operand data type date is invalid for subtract operator.
我不知道为什么

hara是一种功能:

CREATE FUNCTION Salman( @Date  date )
RETURNS TABLE 
AS
RETURN 
(

SELECT TOP 1000 [ID]
      ,[Name]
      ,[LastDate]
      ,[Rule]
      ,[CoA]
  FROM [Scheduling_Employee].[dbo].[Group]
  where ([LastDate]-@Date)%[Rule]=0  
)
GO
您可以尝试使用

因此,在您的情况下,您可能会这样改变:

where DATEDIFF(dd,LastDate,@Date)%[Rule]=0
               ^^--Change this to mm,qq whatever you want.
试试这个:

CREATE FUNCTION Salman( @Date  date )
RETURNS TABLE 
AS
RETURN 
(

SELECT TOP 1000 [ID]
  ,[Name]
  ,[LastDate]
  ,[Rule]
  ,[CoA]
FROM [Scheduling_Employee].[dbo].[Group]
where (Datediff(dd,[LastDate],@Date))%[Rule]=0  
)
GO

我相信你们两个约会的形式不完全一样。这应该行得通

CREATE FUNCTION Salman( @Date  date )
RETURNS TABLE 
AS
RETURN 
(

SELECT TOP 1000 [ID]
      ,[Name]
      ,[LastDate]
      ,[Rule]
      ,[CoA]
  FROM [Scheduling_Employee].[dbo].[Group]
  where (cast([LastDate] as datetime)-cast(@Date as datetime))%[Rule]=0  
)
GO

如果他想做<代码> [LasDeDe] -@日期<代码>,我们不应该考虑<代码> @日期<代码> = DATEDIFS的<代码> SistDATE < /C>?因此,查询将是
其中DATEDIFF(dd,@Date,LastDate)%[Rule]=0
CREATE FUNCTION Salman( @Date  date )
RETURNS TABLE 
AS
RETURN 
(

SELECT TOP 1000 [ID]
      ,[Name]
      ,[LastDate]
      ,[Rule]
      ,[CoA]
  FROM [Scheduling_Employee].[dbo].[Group]
  where (cast([LastDate] as datetime)-cast(@Date as datetime))%[Rule]=0  
)
GO