The date of the data type of the operand is not valid for the subtraction operator

I have a field in my table called [LastDate] with DataType Date

. and I'm going to write a function that calculates [LastDate]-@PassedParameter

, but an error occurs:

Operand data type date is invalid for subtract operator.

      

I do not know why?

hara is a function:

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

      

+3


source to share


2 answers


You can try using the DATEDIFF function .

DATEDIFF ( datepart , startdate , enddate )

      



So, in your case, you can change like this:

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

      

+3


source


Try the following:



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

      

0


source







All Articles