将Ageday列转换为空值而不是零

时间:2018-06-15 15:34:35

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

DROP TABLE #bbb

SELECT DISTINCT
    Mrn.PrefixMedicalRecordNumber AS 'Key',
    DATEDIFF(YY,Him.Birthdate,Bar.DischargeDate) AS 'Age',
    CASE 
       WHEN DATEDIFF(YY, Him.Birthdate, Bar.DischargeDate) < '1'
          THEN DATEDIFF(d, Him.Birthdate, Bar.DischargeDate)
          ELSE ''
    END AS 'AgeDay' 

请帮助我,在ageday列中我应该能看到空字符串而不是null或o但现在我看到0

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT

      Mrn.PrefixMedicalRecordNumber as 'Key' 
    ,DATEDIFF(YY,Him.Birthdate,Bar.DischargeDate) as 'Age'
    ,CASE WHEN DATEDIFF(YY,Him.Birthdate,Bar.DischargeDate) < 1
     THEN ISNULL(NULLIF(CAST(DATEDIFF(d,Him.Birthdate,Bar.DischargeDate) AS VARCHAR(10)) , 0) , '')
     ELSE''
     END as 'AgeDay'