如何获得零而不是null

时间:2014-03-12 02:16:45

标签: sql-server-2008

我的问题是,当它为空时,我必须将此Closing Adv和Closing Debt列显示为零。

SELECT 
  a.StudentCode, c.FullName, a.CentreCode, a.FeesCode,
  a.InstOrYear, c.FullName, d.BranchName, b.TotalPayment, 
  a.BalanceAmt,
  CASE 
     WHEN (b.TotalPayment > a.BalanceAmt) THEN (b.TotalPayment - a.BalanceAmt) 
     WHEN (b.TotalPayment = a.BalanceAmt) then '0'
  END as ClosingAdv,
  CASE 
 WHEN (a.BalanceAmt > b.TotalPayment) THEN (a.BalanceAmt - b.TotalPayment)
 WHEN (b.TotalPayment = a.BalanceAmt) then '0'
  END as ClosingDebt

1 个答案:

答案 0 :(得分:0)

您可以在语句中添加其他案例,也可以按照上面的建议使用ISNULL()

SELECT 
  a.StudentCode, c.FullName, a.CentreCode, a.FeesCode,
  a.InstOrYear, c.FullName, d.BranchName, b.TotalPayment, 
  a.BalanceAmt,
  CASE 
     WHEN (b.TotalPayment > a.BalanceAmt) THEN (b.TotalPayment - a.BalanceAmt) 
     WHEN (b.TotalPayment = a.BalanceAmt) then '0'
     WHEN b.TotalPayment is NULL then '0'
  END as ClosingAdv,
  CASE 
 WHEN (a.BalanceAmt > b.TotalPayment) THEN (a.BalanceAmt - b.TotalPayment)
 WHEN (b.TotalPayment = a.BalanceAmt) then '0'
 WHEN a.BalanceAmt is NULL then '0'
  END as ClosingDebt