SQL零不会出现在查询中

时间:2014-02-18 10:10:30

标签: sql sql-server

说我在我的sql中有这个查询...

DECLARE @earninglist varchar(1000)
       Set @earninglist=STUFF((SELECT DISTINCT '],['
            +LTRIM([Description]) FROM
            PR_Earnings 
            ORDER BY '],[' + LTRIM([Description])
                        FOR XML PATH('')
                        ), 1, 2, '')+']'


DECLARE @chargeslist varchar(1000)
    Set @chargeslist=STUFF((SELECT DISTINCT '],['
            +LTRIM([ChargesTypeName]) FROM
            PRChargesAdvancesTypes 
            ORDER BY '],[' + LTRIM([ChargesTypeName])
                        FOR XML PATH('')
                        ), 1, 2, '')+']'
  Declare @sql varchar(max)
  Set @sql=
   'Select Name,BranchId,Basic,'+@earninglist+',Overtime,[Paid Hol],[Sunday OT],
     [Gross Amount],[WithTax],[SSS Cont],[SSS Loan],[Pagibig Cont],[Pagibig Loan]
     ,Medicare,'+@chargeslist+',[Total Ded],[Net with OD],[Net no OD]
    from
     (
      Select distinct 
        pr_employees.Fullname as Name
       ,PR_Employees.BranchID,PR_payroll.BasicPay as Basic
       ,PR_Empearnings.EarningAmt
       ,PR_Earnings.Description
       ,pr_payroll.Overtime
       ,Pr_payroll.SundayOT as [Sunday OT]
       ,Pr_Payroll.PaidHol as [Paid Hol]
       ,pr_payroll.ThirteenthMonthPay as [Thirteen MO]
       ,pr_payroll.Grosspay as [Gross Amount]
       ,pr_payroll.WithHoldingTax as [WithTax]
       ,pr_payroll.SSSPremium as [SSS Cont]
       ,pr_payroll.SSSLoan as [SSS Loan]
       ,pr_payroll.PagibigPremium as [Pagibig Cont]
       ,pr_payroll.PagibigLoan as [Pagibig Loan]
       ,pr_payroll.NHIPPremium as Medicare
       ,pr_payroll.TotalDeductions as [Total Ded]
       ,pr_Payroll.netpay as [Net with OD]
       ,pr_payroll.netnoOd as [Net no OD]
       ,prchargesAdvances.Credit
       ,prchargesadvancesTypes.ChargesTypeName
     from pr_employees
      left join pr_payroll on PR_Employees.EmpID=PR_Payroll.EmpID
      left join PR_EmpEarnings on PR_Payroll.EmpID=PR_EmpEarnings.EmpID
      left join PR_Earnings on PR_EmpEarnings.EarningId=pr_earnings.earningid
      left join PR_Overtime on PR_Overtime.EmpID=PR_Payroll.EmpID
      left join PRChargesAdvances on PRChargesAdvances.transactiondate=pr_payroll.period   
       and prchargesadvances.empid=pr_payroll.empid
      left join PRChargesAdvancesTypes on PRChargesAdvances.ChargeTypeID=PRChargesAdvancesTypes.ChargesTypeID
     where PR_Payroll.Period=''8/31/2013''  
  ) as Source
  pivot 
  (
     MAX([EarningAmt])
     FOR [Description] IN ('+@earninglist +') 
  ) as pvt
   pivot 
  (
     MAX([Credit])
     FOR [ChargesTypeName] IN ('+@chargeslist +') 
  ) as pvt2
  ;'
  EXEC (@sql)

这个的输出是:

Name    BranchID     Basics     Allowance    Cola    incentives  
 name1      1        5000                    200       250
 name2      2        3000        250                   100

有没有办法可以在津贴和可乐领域中把零而不是空白?

请帮助非常需要它,谢谢..

2 个答案:

答案 0 :(得分:2)

您要做的是将EarningAmt NULL值替换为0
您可以使用ISNULL完成此操作:

ISNULL(MAX([EarningAmt]),0)

基本上,如文档中所述,它:

  

用指定的替换值替换NULL。

您案件中的replacement value0

答案 1 :(得分:1)

尝试;

ISNULL(MAX([EarningAmt]),0)