过滤掉查询中的NULL

时间:2014-02-27 19:57:19

标签: sql null filtering

以下子查询返回一些空值和一些值。我想选择非空行,我该怎么做?

        ,(select distinct LEDGERTABLE.ACCOUNTNAME
    FROM ledgertrans 
    Where Salesline.SalesID = #tempCMs.SALESID and salesline.DIMENSION = 
        (Select top 1 Ledgertrans.Dimension
        From LedgerTrans
            Where Ledgertrans.Voucher = #tempCms.InvoiceID and LedgerTrans.AccountNum = Ledgertable.Accountnum)
 ) As 'Account'

由于

2 个答案:

答案 0 :(得分:3)

使用NULL

过滤IS NOT NULL

试试这个:

(select distinct LEDGERTABLE.ACCOUNTNAME
    FROM ledgertrans 
    Where Salesline.SalesID = #tempCMs.SALESID and salesline.DIMENSION = 
        (Select top 1 Ledgertrans.Dimension
        From LedgerTrans
            Where Ledgertrans.Voucher = #tempCms.InvoiceID 
            and LedgerTrans.AccountNum = Ledgertable.Accountnum
            AND LEDGERTABLE.ACCOUNTNAME IS NOT NULL)
 ) As 'Account'

NULL使用相等性测试(!= NULL)进行过滤,因为NULL值未知,因此无法确定相等性。

答案 1 :(得分:2)

AND LEDGERTABLE.ACCOUNTNAME IS NOT NULL添加到您的WHERE子句中。