在 SQL 服务器中用 IIF 替换大小写

Replacing case with IIF in SQL Server

SELECT 
    patientname, patientinsurance, patienthistory,
    (CASE 
        WHEN patientinsurance IS NOT NULL THEN 'insured'
        WHEN CDPrintBatchId IS NULL THEN 'uninsured'    
     END) AS insurancestatus
FROM 
    [dbo]..database
WHERE
    admissiontime BETWEEN '2020-09-03' AND '2020-09-04'

所以对于我的上述查询,我​​想使用 IIF 而不是 CASE,因为我想只显示没有保险的患者,是否正确?因为如果我在我的案例场景中只使用一个案例,那么 null 应该出现在我的 insurancestatus 列中而不是 uninsured。

SELECT 
    IIF(patient("Null") = 0, 'YES', 'NO');

基本上我只想在结果中显示 null 的结果,不包括 not null

所以您要删除“未保险”的所有记录?

那么为什么不对保险状态的未保险进行硬编码并使用 where 子句限制来删除未保险记录。

SELECT patientname, patientinsurance, patienthistory, 
  CASE WHEN patientinsurance is null then 'uninsured' else 'insured' end as InsuranceStatus
FROM [dbo]..database
WHERE admissiontime between '2020-09-03' and '2020-09-04'
  AND 

此查询等同于发布的查询,但在所有 FALSE 情况下 returns 'uninsured'。 insurancestatus 不会为 NULL

SELECT patientname, patientinsurance, patienthistory,
        iif(patientinsurance is not null, 'insured', 'uninsured') insurancestatus 
FROM [dbo]..database
where admissiontime between '2020-09-03' and '2020-09-04';