SQL Server - 如果两个特定字段都为空,则从视图中排除记录

发布于 2024-10-26 17:44:54 字数 308 浏览 1 评论 0原文

我正在 SQL Server 中编写一个视图,并且需要对结果进行过滤,以便如果记录上的两个特定字段具有空值,则该记录将被排除。

例如,表 Customer 具有字段 Code、Name、Address、Payment、Payment_Date

如果付款 & Payment_Date 为空,则从结果集中排除该记录,但是,如果只有一个为空(或任何其他字段),则可以返回该记录。

SQL Server 中可以实现此功能吗?任何帮助将不胜感激。

I'm writing a view in SQL Server and I need the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-11-02 17:44:54
SELECT  *
FROM    mytable
WHERE   other_conditions
        …
        AND (payment IS NOT NULL OR payment_date IS NOT NULL)
SELECT  *
FROM    mytable
WHERE   other_conditions
        …
        AND (payment IS NOT NULL OR payment_date IS NOT NULL)
殊姿 2024-11-02 17:44:54

合并两个字段,并检查 null 值,

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

这比一串 OR 子句更容易看懂(恕我直言)

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)

我三岁 2024-11-02 17:44:54

您可以在 where 子句中执行此操作,只需将其翻转并使用 OR 代替:

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

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...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文