Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 添加与IN运算符组合的CASE语句时出错;操作数类型冲突:日期与INT不兼容;_Sql_Sql Server - Fatal编程技术网

Sql 添加与IN运算符组合的CASE语句时出错;操作数类型冲突:日期与INT不兼容;

Sql 添加与IN运算符组合的CASE语句时出错;操作数类型冲突:日期与INT不兼容;,sql,sql-server,Sql,Sql Server,我有一个SQL查询,它试图创建与时间有关的分组,例如会计季度和会计期间等 当我执行查询时,返回一个错误,并显示以下消息 操作数类型冲突:日期与int不兼容 我多次尝试以不同的方式重写查询,但不幸的是,由于我对这个主题没有足够的知识,我似乎无法找出原因。然而,我确实认为错误的发生是由于CASE语句与IN结合编写的 我的代码如下 SELECT d1.*, case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 'H1'

我有一个SQL查询,它试图创建与时间有关的分组,例如会计季度和会计期间等

当我执行查询时,返回一个错误,并显示以下消息

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

我多次尝试以不同的方式重写查询,但不幸的是,由于我对这个主题没有足够的知识,我似乎无法找出原因。然而,我确实认为错误的发生是由于CASE语句与IN结合编写的

我的代码如下

SELECT d1.*,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 'H1'
            when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 'H2'
            else NULL
       end  AS FISCAL_HALF_YEAR_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 1
            when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 2
            else NULL
       end  AS FISCAL_HALF_YEAR,
       rep_acc_period as FISCAL_PERIOD,
       cast (rep_acc_period as nvarchar) as FISCAL_PERIOD_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3) then 'Q1'
            when isnull(d1.rep_acc_period,0) in (4,5,6) then 'Q2'
            when isnull(d1.rep_acc_period,0) in (7,8,9) then 'Q3'
            when isnull(d1.rep_acc_period,0) in (10,11,12) then 'Q4'
            else NULL
       end  AS FISCAL_QUARTER_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3) then 1
            when isnull(d1.rep_acc_period,0) in (4,5,6) then 2
            when isnull(d1.rep_acc_period,0) in (7,8,9) then 3
            when isnull(d1.rep_acc_period,0) in (10,11,12) then 4
            else NULL
       end  AS FISCAL_QUARTER,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4) then 1
            when isnull(d1.rep_acc_period,0) in (5,6,7,8) then 2
            when isnull(d1.rep_acc_period,0) in (9,10,11,12) then 3
            else NULL
       end  AS FISCAL_TERTIAL,
       rep_acc_year as FISCAL_YEAR,
       rep_acc_year * 100 + rep_acc_period as FISCAL_YEAR_PERIOD,
       id as IS_ID   
   FROM TIME_TAB d1
所选列的数据类型如下所示

SELECT d1.*,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 'H1'
            when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 'H2'
            else NULL
       end  AS FISCAL_HALF_YEAR_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4,5,6) then 1
            when isnull(d1.rep_acc_period,0) in (7,8,9,10,11,12) then 2
            else NULL
       end  AS FISCAL_HALF_YEAR,
       rep_acc_period as FISCAL_PERIOD,
       cast (rep_acc_period as nvarchar) as FISCAL_PERIOD_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3) then 'Q1'
            when isnull(d1.rep_acc_period,0) in (4,5,6) then 'Q2'
            when isnull(d1.rep_acc_period,0) in (7,8,9) then 'Q3'
            when isnull(d1.rep_acc_period,0) in (10,11,12) then 'Q4'
            else NULL
       end  AS FISCAL_QUARTER_TXT,
       case when isnull(d1.rep_acc_period,0) in (1,2,3) then 1
            when isnull(d1.rep_acc_period,0) in (4,5,6) then 2
            when isnull(d1.rep_acc_period,0) in (7,8,9) then 3
            when isnull(d1.rep_acc_period,0) in (10,11,12) then 4
            else NULL
       end  AS FISCAL_QUARTER,
       case when isnull(d1.rep_acc_period,0) in (1,2,3,4) then 1
            when isnull(d1.rep_acc_period,0) in (5,6,7,8) then 2
            when isnull(d1.rep_acc_period,0) in (9,10,11,12) then 3
            else NULL
       end  AS FISCAL_TERTIAL,
       rep_acc_year as FISCAL_YEAR,
       rep_acc_year * 100 + rep_acc_period as FISCAL_YEAR_PERIOD,
       id as IS_ID   
   FROM TIME_TAB d1
  • 代表会计期间-比基特
  • 财政半年度-varchar(2)
  • 报告会计期间/会计期间-bigint
  • 会计期间-nvarchar
  • 财政季度文本-varchar(2)
  • 财政司司长
  • 财政(国际)
  • 报告会计年度/会计年度-bigint
  • 会计年度和期间-bigint

这方面有什么帮助吗?

什么数据类型是“d1.rep\u acc\u period”?整数还是日期时间?如果晚一点,这就是为什么。SQL无法将INT与DATETIME进行比较。在SQL Server中,它是一个
CASE
expression not语句。如果希望我们提供帮助,我们需要知道您选择的所有列的数据类型。但是,您可以通过注释掉除一个大小写表达式以外的所有内容,然后一次处理一个内容来确定引起问题的列,从而找出有问题的列。@DaleK。在所有数据库中,它都是一个
case
表达式。某些数据库脚本语言确实支持
case
语句,但在
select
查询中总是作为表达式使用。@Fandango68 d1.rep\u acc\u period的数据类型是bigint。什么数据类型是“d1.rep\u acc\u period”?整数还是日期时间?如果晚一点,这就是为什么。SQL无法将INT与DATETIME进行比较。在SQL Server中,它是一个
CASE
expression not语句。如果希望我们提供帮助,我们需要知道您选择的所有列的数据类型。但是,您可以通过注释掉除一个大小写表达式以外的所有内容,然后一次处理一个内容来确定引起问题的列,从而找出有问题的列。@DaleK。在所有数据库中,它都是一个
case
表达式。某些数据库脚本语言确实支持
case
语句,但在
select
查询中总是作为表达式使用。@Fandango68 d1.rep\u acc\u period的数据类型是bigint。