[英]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.
为此,我使用 ALTER 命令进行了添加。 Then I wanted to return the number of months remaining until the end of membership with a function, but I got the error
然后我想用 function 返回会员资格结束前剩余的月数,但我得到了错误
Operand type clash: int is incompatible with date.
操作数类型冲突:int 与日期不兼容。
I am getting the error while trying to create the function. This is exactly how it is:我在尝试创建 function 时遇到错误。这正是它的样子:
Msg 206, Level 16, State 2, Procedure getMembershipExpiration, Line 1 Operand type clash: int is incompatible with date
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.作为解决方案,建议将日期部分放在引号中并以 YYYY-MM-DD 模式编写,但它并没有引导我找到解决方案。
DATEDIFF
returns int
, not date
. DATEDIFF
返回int
,而不是date
。 Replace line #5 with:将第 5 行替换为:
RETURNS int
and line #12 with:和第 12 行:
DECLARE @remainingMonth int = (SELECT DATEDIFF(MONTH,@nnow ,@endDate ))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.