我在视图中具有以下CTE,我想添加一些NULLIF(或等效功能),以便如果WTHD_BOX16
为NULL
,则替换为空字符串(空白),否则显示值。
WITH VWDETAIL AS
( SELECT VENDOR_ID , 'PA/13679089' AS WTHD_BOX17 , WTHD_BASIS_AMT AS WTHD_BOX18 , YEAR(PYMNT_DT) AS YEAR ,
CASE WHEN PYMNT_DT <= '01/31/2014' THEN '1099' ELSE 'NGC' END AS WTHD_CNTL_ID
FROM PS_WTHD_TRXN_TBL A
WHERE WTHD_CLASS IN ('01','02','07')
AND BUSINESS_UNIT IN ('10000','50000')
AND PYMNT_DT <= CASE BUSINESS_UNIT WHEN '10000' THEN '01/31/2014' ELSE '12/31/2018' END)
, BOX16DATA AS
( SELECT --A.BUSINESS_UNIT, A.VOUCHER_ID,
A.PYMNT_TYPE, SUM(A.PAID_AMT) AS BOX16PAIDAMT, SUM(A.PYMNT_GROSS_AMT) AS WTHD_BOX16, B.VENDOR_ID, YEAR(A.SCHEDULED_PAY_DT) AS 'Year'
FROM PS_PYMNT_VCHR_XREF A
INNER JOIN PS_VOUCHER B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT AND B.VOUCHER_ID = A.VOUCHER_ID
WHERE PYMNT_TYPE = 'W'
AND REMIT_VENDOR = '47860A'
GROUP BY B.VENDOR_ID, A.PYMNT_TYPE, YEAR(A.SCHEDULED_PAY_DT)
)
SELECT A.VENDOR_ID, B.WTHD_BOX16, WTHD_BOX17 , SUM(WTHD_BOX18) AS WTHD_BOX18 , YEAR , WTHD_CNTL_ID
FROM VWDETAIL A
LEFT OUTER JOIN BOX16DATA B ON B.VENDOR_ID = A.VENDOR_ID AND B.Year = YEAR
GROUP BY A.VENDOR_ID , WTHD_BOX17 , YEAR , WTHD_CNTL_ID, B.WTHD_BOX16
ORDER BY A.YEAR
GO
我在最后的NULLIF
语句中添加了Select
,但是我又收到了错误消息“将数据类型varchar转换为数字时出错”。
SELECT A.VENDOR_ID, NULLIF(B.WTHD_BOX16, ''), WTHD_BOX17 , SUM(WTHD_BOX18) AS WTHD_BOX18 , YEAR , WTHD_CNTL_ID
FROM VWDETAIL A
LEFT OUTER JOIN BOX16DATA B ON B.VENDOR_ID = A.VENDOR_ID AND B.Year = YEAR
GROUP BY A.VENDOR_ID , WTHD_BOX17 , YEAR , WTHD_CNTL_ID, B.WTHD_BOX16
ORDER BY A.YEAR
GO
如何解决此问题,以便在没有NULL且NULL行为空字符串的情况下获得数值?使用MS SQL Server。
答案 0 :(得分:1)
您可以将列转换为varchar,例如强制转换(col为varchar(50))。空白不能转换为数字。