49

Hi I am new to Oracle. How do I do a simple statement, for example get product id from the last 30, or 20 days purchase date?

SELECT productid FROM product
WHERE purchase_date ? 

6 Answers 6

82
SELECT productid FROM product WHERE purchase_date > sysdate-30
4
  • How about this: "between SYSDATE and SYSDATE - 30"??
    – miracle
    Mar 6, 2011 at 13:44
  • or how do I specify the date manually? like BETWEEN '2011-3-3' and '2011-2-3'?
    – miracle
    Mar 6, 2011 at 13:48
  • @miracle select productid FROM product where purchase_date between to_date('2011-02-03','YYYY-MM-DD') AND to_date( '2011-03-03','YYYY-MM-DD') Mar 6, 2011 at 13:52
  • 1
    @miracle: To answer those questions, if using BETWEEN, use BETWEEN SYSDATE-30 AND SYSDATE (the earliest one comes first). To specify a date literal, either use the ANSI date literal date '2011-03-03' or the to_date function e.g., to_date('03-03-2011','DD-MM-YYYY').... or maybe modify your question if you want to know all that... Mar 6, 2011 at 13:55
17

The easiest way would be to specify

SELECT productid FROM product where purchase_date > sysdate-30;

Remember this sysdate above has the time component, so it will be purchase orders newer than 03-06-2011 8:54 AM based on the time now.

If you want to remove the time conponent when comparing..

SELECT productid FROM product where purchase_date > trunc(sysdate-30);

And (based on your comments), if you want to specify a particular date, make sure you use to_date and not rely on the default session parameters.

SELECT productid FROM product where purchase_date > to_date('03/06/2011','mm/dd/yyyy')

And regardng the between (sysdate-30) - (sysdate) comment, for orders you should be ok with usin just the sysdate condition unless you can have orders with order_dates in the future.

10

Pay attention to one aspect when doing "purchase_date>(sysdate-30)": "sysdate" is the current date, hour, minute and second. So "sysdate-30" is not exactly "30 days ago", but "30 days ago at this exact hour".

If your purchase dates have 00.00.00 in hours, minutes, seconds, better doing:

where trunc(purchase_date)>trunc(sysdate-30)

(this doesn't take hours, minutes and seconds into account).

1
  • 3
    "where purchase_date > trunc(sysdate-30)" also does the trick and can use an index if one exists (whereas your expression is difficult to optimize).
    – Codo
    Mar 6, 2011 at 14:00
2

Try this : Using this you can select data from last 30 days

SELECT
    *
FROM
    product
WHERE
    purchase_date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
0
SELECT COUNT(job_id) FROM jobs WHERE posted_date < NOW()-30;

Now() returns the current Date and Time.

1
  • 2
    Oracle doesn't appear to have a NOW() function. I also tried without the parentheses. CURRENT_DATE or SYSDATE may do the job.
    – FloverOwe
    Oct 24, 2022 at 23:20
-1
select status, timeplaced 
from orders 
where TIMEPLACED>'2017-06-12 00:00:00' 

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.