如何在CASE语句中将数字显示字符串显示为“ - ”

时间:2016-09-21 17:02:59

标签: tsql sql-server-2012 null case

我有一个简单的CASE语句,在我的ELSE块数字列中我想显示为' - '。 但它给了我一个错误

Arithmetic overflow error converting varchar to data type numeric.

我该怎么做? 我希望这样: enter image description here

SELECT 
            CASE WHEN ChargeName = 'Premium' THEN CompanyCommissionPercentage  ELSE '-'  END AS CompanyCommissionPercentage
             ,CASE WHEN ChargeName = 'Premium' THEN RemitterCommissionPercentage ELSE '-'  END AS RemitterCommissionPercentage
             ,CASE WHEN ChargeName = 'Premium' THEN RemitterCommission ELSE '-'END AS RemitterCommission
             ,CASE WHEN ChargeName = 'Premium' THEN GrossCommission ELSE '-'END AS GrossCommission
FROM @tmpAccountsPayable 

2 个答案:

答案 0 :(得分:2)

`SELECT 
            CASE WHEN ChargeName = 'Premium' THEN CAST(CompanyCommissionPercentage as varchar(10))  ELSE CAST('-' as varchar(10))  END AS CompanyCommissionPercentage

FROM @tmpAccountsPayable`

答案 1 :(得分:0)

以下是excel中您可以将格式设置为-的图片

你最好不要传递零而不是短划线,并允许excel格式化使0 a - (当然假设输出进入excel)

请注意下面在E20中,当使用会计格式(逗号格式)时,0值将转换为 -

还要注意在会计 - 缩进时,常规短划线如何左对齐。

enter image description here