运行 case 语句时转换失败错误
Conversion failed error when running case statement
当我 运行 以下 case 语句时,出现错误“将 varchar 值 'NO' 转换为数据类型 int 时转换失败”。有人可以告诉我我在这里做错了什么吗?谢谢
,CASE
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) > 0
THEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10))
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) < 0
THEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10))
ELSE 'NO'
END AS DAYS_BEFORE_DUE_DATE
在case
语句中只能返回一种数据类型。但是您可以使用 NULL 而不是 NO
.
在这种情况下,您可以像这样转换 DATEDIFF
函数的结果:
select CASE WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) > 0
THEN convert(varchar, DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)))
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) < 0
THEN convert(varchar, DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)))
ELSE 'NO'
END AS DAYS_BEFORE_DUE_DATE
from Deliveries
正如 Larnu 的评论所表明的那样,问题是 case
表达式 中的混合数据类型之一。 (TSQL 没有 case
语句 。)
猜测:Due_Date
和 OB_Delivery_Date
可能是 Date
列。如果是这样,您可以简单地使用 DateDiff
来计算天数的差异:
DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date )
由于两个 then
子句 return 具有相同的值,因此可以将它们合并:
case when DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) != 0
then Cast( DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) as VarChar(3) )
else 'NO' end as Days_Before_Due_Date
注意Cast
用于将数值转换为字符串
如果你想处理 early/on time/late 你可以使用 Sign
:
case Sign( DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) )
when 1 then 'Late'
when 0 then 'On Time'
when -1 then 'Early'
end as ScheduleStatus
当我 运行 以下 case 语句时,出现错误“将 varchar 值 'NO' 转换为数据类型 int 时转换失败”。有人可以告诉我我在这里做错了什么吗?谢谢
,CASE
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) > 0
THEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10))
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) < 0
THEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10))
ELSE 'NO'
END AS DAYS_BEFORE_DUE_DATE
在case
语句中只能返回一种数据类型。但是您可以使用 NULL 而不是 NO
.
在这种情况下,您可以像这样转换 DATEDIFF
函数的结果:
select CASE WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) > 0
THEN convert(varchar, DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)))
WHEN DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)) < 0
THEN convert(varchar, DATEDIFF(DAY, convert(VARCHAR, Deliveries.DUE_DATE, 10), convert(VARCHAR, DELIVERIES.OB_DELIVERY_DATE, 10)))
ELSE 'NO'
END AS DAYS_BEFORE_DUE_DATE
from Deliveries
正如 Larnu 的评论所表明的那样,问题是 case
表达式 中的混合数据类型之一。 (TSQL 没有 case
语句 。)
猜测:Due_Date
和 OB_Delivery_Date
可能是 Date
列。如果是这样,您可以简单地使用 DateDiff
来计算天数的差异:
DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date )
由于两个 then
子句 return 具有相同的值,因此可以将它们合并:
case when DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) != 0
then Cast( DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) as VarChar(3) )
else 'NO' end as Days_Before_Due_Date
注意Cast
用于将数值转换为字符串
如果你想处理 early/on time/late 你可以使用 Sign
:
case Sign( DateDiff( day, Deliveries.Due_Date, Deliveries.OB_Delivery_Date ) )
when 1 then 'Late'
when 0 then 'On Time'
when -1 then 'Early'
end as ScheduleStatus