简体   繁体   中英

SQL Server 'Operand type clash: int is incompatible with date' error

I wanted to create a membership limit to the database. For this, I added using the ALTER command. Then I wanted to return the number of months remaining until the end of membership with a function, but I got the error

Operand type clash: int is incompatible with date.

I am getting the error while trying to create the function. This is exactly how it is:

Msg 206, Level 16, State 2, Procedure getMembershipExpiration, Line 1 Operand type clash: int is incompatible with date

ALTER TABLE Member
ADD Membership_End_Date DATE DEFAULT '2099-01-01' not null

CREATE FUNCTION getMembershipExpiration(@Member_ID int)
RETURNS date
AS
BEGIN
    DECLARE @endDate date = (SELECT Membership_End_Date FROM Member WHERE Member_ID =@Member_ID)
    
    DECLARE @nnow date = (SELECT GETDATE())
    
    DECLARE @remainingMonth date = (SELECT DATEDIFF(MONTH,@nnow ,@endDate ))
    
    RETURN @remainingMonth
END

As a solution, it was suggested to put the date part in quotation marks and write it in the YYYY-MM-DD pattern, but it did not lead me to the solution.

DATEDIFF returns int , not date . Replace line #5 with:

RETURNS int

and line #12 with:

DECLARE @remainingMonth int = (SELECT DATEDIFF(MONTH,@nnow ,@endDate ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM