检查空列值,以防和忽略它们

时间:2015-02-06 13:52:58

标签: tsql sql-server-2012

我编写了以下查询来检查重复项。

select s.*,m.IsMember_Ind,
        case    when REPLACE(s.[ Owner Mobile],' ','')
                    in (select cd.Mobile 
                        from Company_Detail cd
                        inner join MEMBERSHIP m
                        on cd.Company_ID = m.Company_ID
                        where m.IsMember_Ind = 1)
                    then 'Match'            
                else ' ' end as OwnerMobileMatch,
        case when REPLACE(s.[ Contact Number],' ','')
                    in (select cd.Mobile 
                        from Company_Detail cd
                        inner join MEMBERSHIP m
                        on cd.Company_ID = m.Company_ID
                        where m.IsMember_Ind = 1)
                    then 'Match' else ' ' end as ContactMobileMatch             
from COMPANY c
inner join surreynonmembers s
on c.TTR_ID = s.[Company ID]
left outer join MEMBERSHIP m
on m.Company_ID = c.Company_ID

我的问题是,当找到一个我不需要的空值时,case语句返回匹配。如果只有值,我该如何修改此查询以返回?

1 个答案:

答案 0 :(得分:1)

你试过这个吗?

GO
SELECT s.*
    ,m.IsMember_Ind
    ,CASE 
        WHEN REPLACE(s.[ Owner Mobile], ' ', '') IN (
                SELECT cd.Mobile
                FROM Company_Detail cd
                INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
                WHERE m.IsMember_Ind = 1
                AND ISNULL(s.[ Owner Mobile], '') <> ''
                )
            THEN 'Match'
        ELSE ' '
        END AS OwnerMobileMatch
    ,CASE 
        WHEN REPLACE(s.[ Contact Number], ' ', '') IN (
                SELECT cd.Mobile
                FROM Company_Detail cd
                INNER JOIN MEMBERSHIP m ON cd.Company_ID = m.Company_ID
                WHERE m.IsMember_Ind = 1
                AND ISNULL(s.[ Contact Number], '')<> ''
                )
            THEN 'Match'
        ELSE ' '
        END AS ContactMobileMatch
FROM COMPANY c
INNER JOIN surreynonmembers s ON c.TTR_ID = s.[Company ID]
LEFT JOIN MEMBERSHIP m ON m.Company_ID = c.Company_ID

GO