Click to See Complete Forum and Search --> : SQL Date - Select records in past 30 days ?


invision
09-10-2006, 07:52 AM
Hello,

How do I only display records solely from the past 30 days with an SQL query, using the 'date' field in my database?

The column in the table is called 'date' and uses date type :

date date No 0000-00-00

BuezaWebDev
09-10-2006, 12:26 PM
SELECT *
FROM `tb_table`
ORDER BY tb_table.date DESC
LIMIT 30

invision
09-10-2006, 02:17 PM
Thanks.

I'll try it out now.

I had been suggested this DATE_SUB(CURDATE(),INTERVAL 30 DAY), but it didn't work

NogDog
09-10-2006, 02:38 PM
It would probably be best to let us know what DBMS you're using, as things like the DATE_SUB() function might work in MySQL, but might not in other DBMS's that do not have a function of that name.

invision
09-10-2006, 04:01 PM
It's MySQL.

Thanks for looking.

BuezaWebDev
09-10-2006, 05:03 PM
Yeah, date math would work, but I honestly have no clue which ones would be more efficient in terms of SQL statement execution.

invision
09-10-2006, 06:45 PM
I'm not so sure how to do this.

Can you be more specific, preferrably an example.

chazzy
09-12-2006, 06:25 AM
Thanks.

I'll try it out now.

I had been suggested this DATE_SUB(CURDATE(),INTERVAL 30 DAY), but it didn't work

well how exactly did you use it?

select * from table WHERE date_column BETWEEN CURDATE() and DATE_SUB(CURDATE(),INTERVAL 30 DAY)

Should do it.

archie_ait
04-02-2012, 12:30 PM
What should be the query if my database is Oracle?
QUES: What will be the oracle query to fetch records for last 30 days?
Thanks in advance.