Problem solve Get help with specific problems with your technologies, process and projects.

Writing a query to return invoices that are 30 or 60 days past due

How would I write an SQL query that selects only those invoices that are 30 days or 60 days past due?

I'll assume your table looks like this:

create table Invoices
( InvoiceNo number(9),
  Amount number(11,2),
  OrderDate date,
  DueDate date

You'll want to use a simple where clause to select only those rows where the date 30 (or 60) days after the due date has passed. You would also use a function that would return the current date. In SQL Server, your SQL statement might look like this:

select * from Invoices 
  where DueDate + 30 <= GETDATE();

If you are using Oracle, it might look like this:

select * from Invoices 
  where DueDate + 30 <= sysdate;

For More Information

  • What do you think about this answer? E-mail the edtiors at [email protected] with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle and SQL