COALESCE不工作?

时间:2014-01-19 14:31:33

标签: sql coalesce

我的P.EMP_PAY_DUE_TO_LEAVE_DATE字段中有NULLS,我尝试使用以下代码消除它们,但NULLS仍然存在!

SELECT DISTINCT V.EMP_CODE
, CASE 
    WHEN V.HIST_PERIOD < 10 
    THEN 
        CAST(V.HIST_YEAR AS VARCHAR)     + RIGHT('0' + CAST (V.HIST_PERIOD + 3 AS VARCHAR), 2) 
    ELSE 
        CAST(V.HIST_YEAR + 1 AS VARCHAR) + RIGHT('0' + CAST (V.HIST_PERIOD - 9 AS VARCHAR) ,2) 
  END AS PAYPERIOD
, V.Department_Id
, CASE 
    WHEN MONTH (P.EMP_PAY_DUE_TO_LEAVE_DATE) >= MONTH(DATEADD(M, -1, GETDATE()) 
    OR P.EMP_PAY_DUE_TO_LEAVE_DATE IS NULL 
    THEN 
        COALESCE
        (
            CONVERT
            (
                DECIMAL(10, 2)
                , V.EMP_SORT_DESC
            )
            , 0
        ) / 37.5
    END AS FTE

我看不出我的代码有什么问题,但显然有!有谁能看到这个问题?

1 个答案:

答案 0 :(得分:3)

你的上一次......

CASE
  WHEN (MONTH (P.EMP_PAY_DUE_TO_LEAVE_DATE) >= MONTH(DATEADD(M, -1, GETDATE())) OR P.EMP_PAY_DUE_TO_LEAVE_DATE IS NULL) 
    THEN (COALESCE (CONVERT (DECIMAL (10, 2) ,V.EMP_SORT_DESC), 0) / 37.5) END AS FTE

没有ELSE案例,如果NULL案例不适用,则这种情况会变为THEN