简体   繁体   English

操作数类型冲突:日期与int不兼容?

[英]Operand type clash: date is incompatible with int?

Can someone help me out here, I have the following TSQL that is throwing errors (it is part of a table view). 有人可以在这里帮我吗,我有以下TSQL引发错误(它是表视图的一部分)。

SELECT MAX([WEEK]) FROM MyTable WHERE [Year] = DATEPART(YYYY, GETDATE())

This is part of a view case statement as such: 这是一个View Case语句的一部分,例如:

CASE 
    WHEN [WEEK] <= (SELECT MAX([WEEK]) FROM MyTable WHERE [Year] = DATEPART(YYYY, GETDATE())) THEN 
        [YEAR]
END YTD,

This throws an error: Operand type clash: date is incompatible with int 这将引发错误:操作数类型冲突:日期与int不兼容

The table is defined as: 该表定义为:

CREATE TABLE [dbo].[MyTable](
  [Week] [tinyint] NULL,
  [Year] [date] NULL,
) ON [PRIMARY]
GO

How can I make the TSQL work without changing the table field type? 如何在不更改表字段类型的情况下使TSQL工作?

The [Year] field contains records in the format of YYYY-MM-DD. [年份]字段包含YYYY-MM-DD格式的记录。

This is on a SQL Server 2016. 这是在SQL Server 2016上。

Calling a date column year is a very poor naming choice. dateyear称为一个非常差的命名选择。
The SQL statement you've posted seems entirely reasonable - since when you see a column named year you expect it to contain the year (meaning, an int), not to contain a date value. 您发布的SQL语句似乎是完全合理的-因为当您看到名为year的列时year您希望它包含Year(意思是int),而不包含日期值。

You should think of renaming that column or changing it's data type, just to save some time for the poor bastard that's going to have to do maintenance work on this code a year from now, since it's probably going to be you. 您应该考虑重命名该列或更改其数据类型,只是为了节省可怜的混蛋的时间,这些混蛋必须从现在开始每年对此代码进行维护工作,因为可能是您自己。

Than being said, the Year() function returns an int value - and naturally, such a value can't be compared to a date value - How would you compare 532 with May 1st 2019? 比起说的话, Year()函数返回一个int值-自然地,这样的值不能与日期值进行比较-您如何将532与2019年5月1日进行比较? that's comparing apples to oranges. 比较苹果和橘子。

You have a SARGable option and a non-SARGable option to fix this problem. 您有一个SARGable选项和一个非SARGable选项来解决此问题。
In a nutshell - SARGable means a condition that allows the database engine to use an index seek, when a relevant index exists. 简而言之,SARGable表示在相关索引存在时允许数据库引擎使用索引查找的条件。

The non-SARGable option is what John Cappelletti suggested in his comment - use datepart for both the column and the value: 约翰·卡佩莱蒂(John Cappelletti)在评论中建议使用non-SARGable选项-将datepart用于列和值:

 WHERE datepart(YEAR,[Year]) = DATEPART(YEAR, GETDATE())

This is a good option if you don't have an index in the year column or the table is not very big so an index scan vs an index seek is not really an issue, or if select speed is not an issue. 如果您在year列中没有索引,或者表不是很大,那么索引扫描与索引查找实际上不是问题,或者选择速度不是问题,那么这是一个不错的选择。

The SARGable option is to use a slightly different condition - like HABO suggsted in his comment (though I think my version is slightly simpler): SARGable选项使用的条件略有不同-就像HABO在他的评论中建议的 (尽管我认为我的版本稍微简单一些):

WHERE [Year] => DATEFROMPARTS(DATEPART(YEAR, GETDATE()), 1, 1)
AND [Year] < DATEFROMPARTS(DATEPART(YEAR, GETDATE()) + 1, 1, 1)

This is a better option when you have an index on the Year column and the table is large enough so that index scan vs index seek would have a significant enough impact on the select performance. 当“ Year列上有一个索引并且该表足够大时,这是一个更好的选择,这样索引扫描与索引查找将对选择性能产生足够大的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复错误 - 操作数类型冲突:日期与 int 不兼容 - How to fix the error - Operand type clash: date is incompatible with int 操作数类型clash int与sql server中的日期不兼容 - operand type clash int is incompatible with date in sql server SQL 服务器“操作数类型冲突:int 与日期不兼容”错误 - SQL Server 'Operand type clash: int is incompatible with date' error SQL Server 2012(触发器)操作数类型冲突:int与日期不兼容 - SQL Server 2012 (triggers) Operand type clash: int is incompatible with date 存储过程操作数类型冲突:日期与int不兼容 - Stored Procedure Operand type clash: date is incompatible with int 操作数类型冲突:日期与用CONVERT()解决的int不兼容...为什么? - Operand type clash: date is incompatible with int solved with CONVERT() … but WHY? 如何解决操作数类型冲突:date is incompatible with int - How to resolve Operand type clash: date is incompatible with int SQL错误IS操作数类型冲突:int与日期不兼容 - SQL ERROR IS Operand type clash: int is incompatible with date SQL Server Operand类型冲突:日期与int不兼容 - Sql Server Operand type clash: date is incompatible with int SQL语句不起作用 - “操作数类型冲突:日期与int不兼容” - SQL statement not working - "Operand type clash: date is incompatible with int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM