# Getting data from database based on date condition

**URL:** <https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767>\
**Category:** Rails\
**Created:** [December 8, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767 "2006-12-08T12:35:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 8, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/1 "2006-12-08T12:35:20Z")

</div>

Hi Everyone,  
this is the statement i have, to get all the contents posted from the  
database,  
@content = Content.find\_all  
Now, i want to retrieve data (content posted) only for the last 5 days  
including current date. Can anyone help me??  
My column name is “createdOn” with both Date and Time values stored.

Thanks in advance,  
Regards,  
Vasanth

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 8, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/2 "2006-12-08T12:58:31Z")

</div>

Hi,  
Use from date\_select and to date\_select,  
and in the controller write the query  
@content=Content.find\_by \_sql(“select \* from table where created\_on  
between to\_date(‘2003/01/01’, ‘yyyy/mm/dd’)and to\_date (‘2003/12/31’,  
‘yyyy/mm/dd’)”)

some thing like this… i hope it will work…

Vasanthakumar C. wrote:

> Hi Everyone,  
> this is the statement i have, to get all the contents posted from the  
> database,  
> @content = Content.find\_all  
> Now, i want to retrieve data (content posted) only for the last 5 days  
> including current date. Can anyone help me??  
> My column name is “createdOn” with both Date and Time values stored.
> 
> Thanks in advance,  
> Regards,  
> Vasanth

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 8, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/3 "2006-12-08T16:09:30Z")

</div>

Naga harish Kanegolla wrote:

> Hi,  
> Use from date\_select and to date\_select,  
> and in the controller write the query  
> @content=Content.find\_by \_sql(“select \* from table where created\_on  
> between to\_date(‘2003/01/01’, ‘yyyy/mm/dd’)and to\_date (‘2003/12/31’,  
> ‘yyyy/mm/dd’)”)
> 
> some thing like this… i hope it will work…
> 
> Vasanthakumar C. wrote:
> 
> > Hi Everyone,  
> > this is the statement i have, to get all the contents posted from the  
> > database,  
> > @content = Content.find\_all  
> > Now, i want to retrieve data (content posted) only for the last 5 days  
> > including current date. Can anyone help me??  
> > My column name is “createdOn” with both Date and Time values stored.
> > 
> > Thanks in advance,  
> > Regards,  
> > Vasanth

this may be a little cleaner:

@content = Content.find(:all, :conditions =\> “created\_on \<  
#{5.days.ago.to\_s(:db)}”)

I havent checked that. You may need to reverse the “\<” operator.

You could also use a range for the time arg. Example:  
@content = Content.find(:all, :conditions =\> “created\_on  
#{(5.days.ago…Time.now).to\_s(:db)}”)

–jake

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 9, 2006, 8:08am UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/4 "2006-12-09T08:08:30Z")

</div>

Wow… its so simple & worked out for me successfully.  
I’ve started liking “Ruby on Rails” more than my old “PHP” code 🙂  
Thanks everyone…

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 8, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/5 "2006-12-08T20:36:04Z")

</div>

Vasanthakumar C. wrote:

> Hi Everyone,  
> this is the statement i have, to get all the contents posted from the  
> database,  
> @content = Content.find\_all  
> Now, i want to retrieve data (content posted) only for the last 5 days  
> including current date. Can anyone help me??  
> My column name is “createdOn” with both Date and Time values stored.
> 
> Thanks in advance,  
> Regards,  
> Vasanth

@content = Content.find(:all,  
:conditions =\> [‘created\_on \> ?’, 5.days.ago])

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 9, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/getting-data-from-database-based-on-date-condition/78767/6 "2006-12-09T13:01:13Z")

</div>

For future reference, if you had named your field created\_on or  
created\_at, rails would have timestamped it for you.
