在SQL中替换空白单元格?

时间:2016-12-06 14:06:01

标签: sql sql-server sql-server-2008

出于某种原因,当这是0时它不会说0或甚至给出一个空值,它只返回任何内容(附加屏幕截图)

我怎样才能让它返回0呢?有人可以解释为什么会这样吗?

select 
    cast(PatientClass as varchar) + ' - ' +  cast([Counted] as varchar) [Procedures Split]
from 
    (select 
         PatientClass, COUNT(*) as [Counted]
     from
         (SELECT                    
              NELCal.LastDayOfWeek, S.FaciltyID, 
              PP.EncounterRecno, PP.ProcedureSequence, 
              S.[AdmissionDate], S.AdmissionConsultantName, S.AdmissionMethod,
              s.LengthOfSpell,
              year(S.[AdmissionDate]) as AdmYear,
              month(S.[AdmissionDate]) as AdmMonth,
              PP.[ProcedureDate],
              year(PP.[ProcedureDate]) as ProcYear,
              month(PP.[ProcedureDate]) as ProcMonth,
              S.Age, S.[AdmissionSpecialty(Function)],
              S.[PatientClass], s.[AdmissionSpecialty(Main)],
              PP.[ProcedureCode], PP.[Procedure]
          FROM 
              [WHREPORTING].[APC].[Spell] S                 
          LEFT JOIN 
              [WHREPORTING].[APC].[Episode] E ON S.SourceSpellNo = E.SourceSpellNo                  
          LEFT JOIN 
              [WHREPORTING].[APC].[AllProcedures] PP ON E.EpisodeUniqueID = PP.EpisodeSourceUniqueID
          LEFT JOIN
              WHREPORTING.APC.Patient P ON P.EncounterRecno = S.EncounterRecno
          JOIN 
              WHREPORTING.LK.Calendar AS NELCal ON PP.ProcedureDate = NELCal.TheDate                    
                                                AND pp.ProcedureDate between '05 dec 2016' and '06 dec 2016'            
            --and NELCal.LastDayOfWeek between DATEADD(DAY,-7*52,cast(GETDATE() as DATE))                   
            --and DATEADD(DAY,-1,cast(GETDATE() as DATE))                   
          WHERE                 
              [AdmissionSpecialty(Function)] = 'Breast Surgery') f          
    group by 
        PatientClass) g

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以使用case声明

执行此操作
CASE WHEN yourfield = ''  THEN 0 ELSE yourfield

如果根本没有返回记录,你可以这样做

IF NOT EXISTS(select 
    cast(PatientClass as varchar) + ' - ' +  cast([Counted] as varchar) [Procedures Split]
from 
    (select 
         PatientClass, COUNT(*) as [Counted]
     from
         (SELECT                    
              NELCal.LastDayOfWeek, S.FaciltyID, 
              PP.EncounterRecno, PP.ProcedureSequence, 
              S.[AdmissionDate], S.AdmissionConsultantName, S.AdmissionMethod,
              s.LengthOfSpell,
              year(S.[AdmissionDate]) as AdmYear,
              month(S.[AdmissionDate]) as AdmMonth,
              PP.[ProcedureDate],
              year(PP.[ProcedureDate]) as ProcYear,
              month(PP.[ProcedureDate]) as ProcMonth,
              S.Age, S.[AdmissionSpecialty(Function)],
              S.[PatientClass], s.[AdmissionSpecialty(Main)],
              PP.[ProcedureCode], PP.[Procedure]
          FROM 
              [WHREPORTING].[APC].[Spell] S                 
          LEFT JOIN 
              [WHREPORTING].[APC].[Episode] E ON S.SourceSpellNo = E.SourceSpellNo                  
          LEFT JOIN 
              [WHREPORTING].[APC].[AllProcedures] PP ON E.EpisodeUniqueID = PP.EpisodeSourceUniqueID
          LEFT JOIN
              WHREPORTING.APC.Patient P ON P.EncounterRecno = S.EncounterRecno
          JOIN 
              WHREPORTING.LK.Calendar AS NELCal ON PP.ProcedureDate = NELCal.TheDate                    
                                                AND pp.ProcedureDate between '05 dec 2016' and '06 dec 2016'            
            --and NELCal.LastDayOfWeek between DATEADD(DAY,-7*52,cast(GETDATE() as DATE))                   
            --and DATEADD(DAY,-1,cast(GETDATE() as DATE))                   
          WHERE                 
              [AdmissionSpecialty(Function)] = 'Breast Surgery') f          
    group by 
        PatientClass) g)

SELECT 0 AS [Procedures Split]
ELSE
select 
    cast(PatientClass as varchar) + ' - ' +  cast([Counted] as varchar) [Procedures Split]
from 
    (select 
         PatientClass, COUNT(*) as [Counted]
     from
         (SELECT                    
              NELCal.LastDayOfWeek, S.FaciltyID, 
              PP.EncounterRecno, PP.ProcedureSequence, 
              S.[AdmissionDate], S.AdmissionConsultantName, S.AdmissionMethod,
              s.LengthOfSpell,
              year(S.[AdmissionDate]) as AdmYear,
              month(S.[AdmissionDate]) as AdmMonth,
              PP.[ProcedureDate],
              year(PP.[ProcedureDate]) as ProcYear,
              month(PP.[ProcedureDate]) as ProcMonth,
              S.Age, S.[AdmissionSpecialty(Function)],
              S.[PatientClass], s.[AdmissionSpecialty(Main)],
              PP.[ProcedureCode], PP.[Procedure]
          FROM 
              [WHREPORTING].[APC].[Spell] S                 
          LEFT JOIN 
              [WHREPORTING].[APC].[Episode] E ON S.SourceSpellNo = E.SourceSpellNo                  
          LEFT JOIN 
              [WHREPORTING].[APC].[AllProcedures] PP ON E.EpisodeUniqueID = PP.EpisodeSourceUniqueID
          LEFT JOIN
              WHREPORTING.APC.Patient P ON P.EncounterRecno = S.EncounterRecno
          JOIN 
              WHREPORTING.LK.Calendar AS NELCal ON PP.ProcedureDate = NELCal.TheDate                    
                                                AND pp.ProcedureDate between '05 dec 2016' and '06 dec 2016'            
            --and NELCal.LastDayOfWeek between DATEADD(DAY,-7*52,cast(GETDATE() as DATE))                   
            --and DATEADD(DAY,-1,cast(GETDATE() as DATE))                   
          WHERE                 
              [AdmissionSpecialty(Function)] = 'Breast Surgery') f          
    group by 
        PatientClass) g

答案 1 :(得分:1)

我猜你的一个子查询没有返回任何记录。这意味着您的结果集将完全为空(甚至不为null)。

如果结果为空,则无法使用这些字段(即您的演员声明)。您需要先检查IF EXISTS ...

答案 2 :(得分:0)

最简单的解决方案是使用Coalesce函数,如下所示:

select Coalesce(Table.Field,0) as columnResult