开发者

SQL Server - Excluding records from a view if two certain fields are both null

I'm writing a view in SQL Server and I need th开发者_JS百科e results filtered so that if two particular fields on a record have null values then that record is excluded.

As an example a table Customer has fields Code, Name, Address, Payment, Payment_Date.

If both Payment & Payment_Date are null then exclude the record from the result set, however if just one is null (or any other field) then it's fine to return the record.

Is this functionality possible in SQL Server?? Any help would be appreciated.


SELECT  *
FROM    mytable
WHERE   other_conditions
        …
        AND (payment IS NOT NULL OR payment_date IS NOT NULL)


Take the coalesceof the two fields, and check that value for null

select * from yourtable where coalesce(field1, field2, field3, etc, etc,) is not null

this is somewhat easier on the eyes than a string of OR clauses (imho)


You can do this in the where clause, simply turn it around and use an OR instead:

WHERE
(
  PAYMENT IS NOT NULL
  OR
  PAYMENT_DATE IS NOT NULL
)
AND
  -- ...rest of where clause here...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜