# Searching between two date

**URL:** <https://www.ruby-forum.com/t/searching-between-two-date/144603>\
**Category:** Rails\
**Created:** [July 31, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/searching-between-two-date/144603 "2008-07-31T12:40:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 31, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/searching-between-two-date/144603/1 "2008-07-31T12:40:49Z")

</div>

Hi  
In model i have (as part of find statement)  
sd.created\_on \<=\> (sd\_ticket\_start\_date…sd\_ticket\_end\_date)

```
(Here i am using ez_where plugin So <=> means BETWEEN)

```

And for example I am passing dates from view like  
\<%= text\_field “sd\_ticket”,  
“end\_date”,:value=\>Time.now.strftime(’%m/%d/%Y’) %\>  
I know created\_on field is saved in the format ‘2008-07-30  
18:22:11.30577’  
So my problem is if I pass both dates(start and end date) equal say  
07/31/2008 from view I dont get the records which are created on  
07/31/2008

So in model how can I format the value in sd\_ticket\_start\_date (  
07/31/2008 ) to the format ‘2008-07-30 18:22:11.30577’ like that.

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 31, 2008, 12:52pm UTC](https://www.ruby-forum.com/t/searching-between-two-date/144603/2 "2008-07-31T12:52:53Z")

</div>

the database is including the time in the search.  
so make sure, that either enddate has a time of 23:59:59  
or is the next day at 0:0:0

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 31, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/searching-between-two-date/144603/3 "2008-07-31T13:26:02Z")

</div>

I looked in log file The query formed is  
SELECT \* FROM service\_desk\_tickets WHERE  
((service\_desk\_tickets.created\_on BETWEEN ‘07/31/2008’ AND  
‘07/31/2008’)) ;

```
Here if I create two ticket in this date(07/31/2008) at 11.30 AM and 

```

11.45 PM for example I dont get it on the search .And to get todays  
record I have to enter startdate as 07/31/2008 and end date 08/01/2008  
(mm/dd/yyyy)

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 31, 2008, 2:03pm UTC](https://www.ruby-forum.com/t/searching-between-two-date/144603/4 "2008-07-31T14:03:10Z")

</div>

The possible solution for this would be I suppose to compare start and  
end before passing it to the sql query.

you would be comparing it for whether start-date is less than end date  
or not. Likewise, have a similarity comparision and if it equals pass  
it on to the query for that respective date alone.

Thanks&Regards,  
Kiran.
