我有一个查询,我必须比较到期日,而且我似乎无法使CASE表达式正常工作。
此刻,我已经注释掉了原来的ELSE子句,我认为可以解决这个问题,但是不幸的是,它给了我一个错误“函数CASE的参数1无效。原因...:数据指定的函数CASE的参数1的类型,长度或值无效。“
有什么想法吗? 我正在使用IBM DB2。
ELSE子句的理想结果是将发票日期(AHINVD)和发票到期日期代码(CMDDCD)相加以获得原始发票到期日期。
Select
CMCMP as CompanyCode
,CMCUS# as CustomerCode
,CMNAME as CustomerName
,CMDDCD as InvoiceDuteDateCode
,AHINV# as InvoiceNumber
,AHISEQ as InvoiceSequence
,AHINVD as InvoiceDate
,AHDUED as InvoiceDueDate
,case
when date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days = date(to_date(RTRIM(AHDUED),'YYYYMMDD')) then NULL
--else date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days
else '12'
end as OriginalInvoiceDueDate
,AHAMT$ as OriginalAmount
,AHCURC as CurrencyCode
from WSDATARG.CUSMASFL cm
left join WSDATARG.ARIHDRFL ar
on CMCMP = AHCMP
and CMCUS# = AHCUS#
where CMCMP = '14'
and AHINVD > '20180918
答案 0 :(得分:0)
尝试CAST(NULL AS DATE)
而不是NULL
答案 1 :(得分:0)
我怀疑您正在使用DB2 for z / OS或DB2 for i。如果是这样,我怀疑它不会喜欢未键入的NULL
。 Db2 LUW将进行锻炼,它必须是DATE。例如
db2 "CREATE TABLE TEST(AHINVD CHAR(8), CMDDCD CHAR(6), AHDUED CHAR(8))"
db2 "describe
SELECT
case
when date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days = date(to_date(RTRIM(AHDUED),'YYYYMMDD')) then NULL
else date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days
end
FROM TEST"
Column Information
Number of columns: 1
SQL type Type length Column name Name length
-------------------- ----------- ------------------------------ -----------
385 DATE 10 1 1
但例如这个
SELECT
case
when date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days = date(to_date(RTRIM(AHDUED),'YYYYMMDD')) then CAST(NULL AS DOUBLE)
else date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days
end
FROM TEST
返回
The data types of the result-expressions of a CASE expression or DECODE function are not compatible.. SQLCODE=-581, SQLSTATE=42804
我猜这与您的Db2 for z / OS消息类似
也许,因为如果您没有ELSE
,则会得到NULL,为什么不直接编写此代码呢?
SELECT
case
when date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days
<> date(to_date(RTRIM(AHDUED),'YYYYMMDD'))
then date(to_date(RTRIM(AHINVD),'YYYYMMDD')) + cast(CMDDCD as INT) days
end
FROM TEST