1

I'm taking a Database Design course online this semester, and this is my first time using SQL. I graduated with a degree in communications, but I'm taking certain computer science classes to help myself. We're using Microsoft SQL Server 2008, and I'm stumped on the last problem of our exercises. First 6 were a breeze (basic select functions, ordering the results, using aliases to rename tables, etc), but the last one deals with null values.

It states:

Write a SELECT statement that determines whether the PaymentDate column of the Invoices table has any invalid values. To be valid, PaymentDate must be a null value if there's a balance due and a non-null value if there's no balance due. Code a compound condition in the WHERE clause that tests for these conditions.

Don't even know where to begin. Ha ha. I typically learn better in a classroom setting, but my schedule would not allow it with this course, so any explanation would help as well! Any help is appreciated!

Dave D.


So which one is correct? It's difficult to break it down when there's two different answers :) On my day off I'm gonna head to the professor's office so she can explain it to me in person anywho lol

4

2 回答 2

1

下面的代码将选择 PaymentDate 无效的记录

SELECT * FROM Invoices WHERE (PaymentDate is not null and BalanceDue is not null) or (PaymentDate is null and BalanceDue is null)
于 2013-02-21T22:20:37.877 回答
1

因为已经发布了一个不正确的答案,所以我将通过这个。

这是一个逻辑问题,即PaymentDateor之一BalanceDue为空。在 SQL 中,您使用表达式测试 NULL IS NULL

所以,这个where子句看起来像:

where (PaymentDate is null and BalanceDue is not null) or -- this is the first clause
      (PaymentDate is not null and BalanceDue is null)    -- this is the second clause

与 NULL 值(=、<>、<、<=、>、>= 或 in)的任何其他比较都返回 NULL 布尔值,这些值被解释为 FALSE。

祝你学习 SQL 好运。

于 2013-02-21T22:26:50.970 回答