在SQL Server中用IIF替换大小写

问题描述

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栏中增加,而不是没有保险的情况。

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

基本上我只希望null的结果出现在结果中,但不排除不为空

解决方法

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

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

@Service
@Transactional
public class MyServiceImpl implements MyService{
...
}
,

此查询与发布的查询等效,只是在所有FALSE情况下返回“未保险”。 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';