# How to select rows between two dates?

**URL:** <https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950>\
**Category:** Rails\
**Created:** [September 6, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950 "2007-09-06T16:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kim\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kim\_G](https://www.ruby-forum.com/u/Kim_G)\
**Post date:** [September 6, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950/1 "2007-09-06T16:19:13Z")

</div>

Hi.

I have a table with two fields, from\_date and to\_date. Now I want to  
do something like this:

SELECT \* FROM table WHERE from\_date \< TODAY AND to\_date \> today;

I’m new to Ruby on Rails, so any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![Kim\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kim\_G](https://www.ruby-forum.com/u/Kim_G)\
**Post date:** [September 6, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950/2 "2007-09-06T16:52:16Z")

</div>

find(:all, :conditions =\> [“from\_data \< ? AND to\_date \> ?”, TODAY,  
today])

I think you’d better read some tutorial books at first. Agile Web  
Development With Rails can be a good start.

---

<div class="post-metadata">

**Author:** ![Kim\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kim\_G](https://www.ruby-forum.com/u/Kim_G)\
**Post date:** [September 6, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950/3 "2007-09-06T17:25:20Z")

</div>

On Sep 6, 2007, at 10:18 AM, Kim G wrote:

> Hi.
> 
> I have a table with two fields, from\_date and to\_date. Now I want to  
> do something like this:
> 
> SELECT \* FROM table WHERE from\_date \< TODAY AND to\_date \> today;
> 
> I’m new to Ruby on Rails, so any help is much appreciated.

class Daterange \< ActiveRecord::Base

# to match your example, not that this should ever be a real name

set\_table\_name ‘table’

def self.as\_of(date=Time.now)  
self.find(:all,  
:conditions =\> [ ‘? BETWEEN from\_date AND to\_date’,  
date.to\_s(:db) ])  
end  
end

Then just call:  
Daterange.as\_of(3.days.from\_now)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Kim\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kim\_G](https://www.ruby-forum.com/u/Kim_G)\
**Post date:** [September 7, 2007, 12:15am UTC](https://www.ruby-forum.com/t/how-to-select-rows-between-two-dates/113950/4 "2007-09-07T00:15:30Z")

</div>

Thanks to both of you.

Rob’s solution was exactly what I was looking for. The SQL and table  
name was only to illustrate the problem.
