简体   繁体   English

SQL Server Operand类型冲突:日期与int不兼容

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

When I try to execute this code I get an error at the 'with DateDimension' line: 当我尝试执行此代码时,在“ with DateDimension”行出现错误:

Msg 206, Level 16, State 2, Line 15 讯息206,第16级,州2,第15行
Operand type clash: date is incompatible with int 操作数类型冲突:日期与int不兼容

This is the SQL query I am using: 这是我正在使用的SQL查询:

declare @DateCalendarStart  date,
        @DateCalendarEnd    date,
        @FiscalCounter      date,
        @FiscalMonthOffset  int;

set @DateCalendarStart = '2011-01-28';
set @DateCalendarEnd = '2012-10-26';


set @FiscalMonthOffset = 3;

with DateDimension //Error got this line 

as

(
    select  @DateCalendarStart as DateCalendarValue,
            dateadd(m, @FiscalMonthOffset, @DateCalendarStart) as FiscalCounter

    union all

    select  DateCalendarValue + 1,
            dateadd(m, @FiscalMonthOffset, (DateCalendarValue + 1)) as FiscalCounter
    from    DateDimension 
    where   DateCalendarValue + 1 < = @DateCalendarEnd
)

Your problem is with the DateCalendarValue + 1 portion. 您的问题是DateCalendarValue + 1部分。 Try using DATEADD() , as below: 尝试使用DATEADD() ,如下所示:

declare @DateCalendarStart  date,
        @DateCalendarEnd    date,
        @FiscalCounter      date,
        @FiscalMonthOffset  int;

set @DateCalendarStart = '2011-01-28';
set @DateCalendarEnd = '2012-10-26';

-- Set this to the number of months to add or extract to the current date to get the beginning 
-- of the Fiscal Year. Example: If the Fiscal Year begins July 1, assign the value of 6 
-- to the @FiscalMonthOffset variable. Negative values are also allowed, thus if your 
-- 2012 Fiscal Year begins in July of 2011, assign a value of -6.
set @FiscalMonthOffset = 3;

with DateDimension 

as

(
    select  @DateCalendarStart as DateCalendarValue,
            dateadd(m, @FiscalMonthOffset, @DateCalendarStart) as FiscalCounter

    union all

    select  DATEADD(DAY, 1, DateCalendarValue), -- Using a DATEADD() function here works for SQL Server
            DATEADD(m, @FiscalMonthOffset, (DATEADD(DAY, 1, DateCalendarValue))) as FiscalCounter
    from    DateDimension 
    where   DATEADD(DAY, 1, DateCalendarValue) < = @DateCalendarEnd
)

SELECT * FROM DateDimension OPTION (MAXRECURSION 1000)

EDIT: I don't know if your original code was going to use the MAXRECURSION option or not, but if you didn't know already I would recommend you read this . 编辑:我不知道您的原始代码是否要使用MAXRECURSION选项,但是如果您不知道,我建议您阅读此内容 Basically, in this circumstance it means that you can list out 1,000 dates with the CTE. 基本上,在这种情况下,这意味着您可以使用CTE列出1,000个日期。 If you need more than that, you'll have to change that 1000 to match your needs. 如果您需要的更多,则必须将其更改为1000以符合您的需求。

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

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