简体   繁体   English

SQL 服务器“操作数类型冲突:int 与日期不兼容”错误

[英]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.

相关问题 SQL 服务器错误操作数类型冲突:int 与日期不兼容 - SQL Server error Operand type clash: int is incompatible with date 操作数类型clash int与sql server中的日期不兼容 - operand type clash int is incompatible with date in sql server SQL Server 2012(触发器)操作数类型冲突:int与日期不兼容 - SQL Server 2012 (triggers) Operand type clash: int is incompatible with date SQL Server Operand类型冲突:日期与int不兼容 - Sql Server Operand type clash: date is incompatible with int SQL错误IS操作数类型冲突:int与日期不兼容 - SQL ERROR IS Operand type clash: int is incompatible with date 操作数类型冲突:日期与sql server中的smallint错误不兼容 - Operand type clash: date is incompatible with smallint error in sql server SQL语句不起作用 - “操作数类型冲突:日期与int不兼容” - SQL statement not working - "Operand type clash: date is incompatible with int' 如何修复错误 - 操作数类型冲突:日期与 int 不兼容 - How to fix the error - Operand type clash: date is incompatible with int 操作数类型冲突:日期与int不兼容? - Operand type clash: date is incompatible with int? 使用日期“'操作数类型冲突:日期与int不兼容'用SQL查询填充Datagrid - Fill Datagrid with Sql Query using Date " 'Operand type clash: date is incompatible with int''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM