# Extract month from date column

**URL:** <https://www.ruby-forum.com/t/extract-month-from-date-column/54013>\
**Category:** Rails\
**Created:** [February 20, 2006, 12:00pm UTC](https://www.ruby-forum.com/t/extract-month-from-date-column/54013 "2006-02-20T12:00:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Akbar](https://www.ruby-forum.com/u/Akbar)\
**Post date:** [February 20, 2006, 12:00pm UTC](https://www.ruby-forum.com/t/extract-month-from-date-column/54013/1 "2006-02-20T12:00:34Z")

</div>

Hi,

I have this rails query:  
@birthdays = Birthday.find :all,  
:conditions =\> ["user\_id = ? and  
birthdate = ‘2006-02-01’, @user\_id],  
:limit =\>  
@birthday\_pages.items\_per\_page,  
:offset =\>  
@birthday\_pages.current.offset

You can ignore the user\_id, limit and offset part. Birthdate is date  
type. That query lists the row which birthdate is 2006-02-01. How do I  
extract month from birthdate column; something like this:  
… birthdate.month = 2 …  
I want to query all the rows which in February or another month.

I have though the solution; something like this:  
… birthdate \>= start\_of\_the\_month and birthdate \<= end\_of\_the\_month  
… ( we find start and end of the month from ruby )  
but maybe rails has simpler way…

Thank you.

---

<div class="post-metadata">

**Author:** ![Akbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Akbar](https://www.ruby-forum.com/u/Akbar)\
**Post date:** [February 20, 2006, 1:23pm UTC](https://www.ruby-forum.com/t/extract-month-from-date-column/54013/2 "2006-02-20T13:23:02Z")

</div>

How do I

> extract month from birthdate column; something like this:  
> … birthdate.month = 2 …

I couldn’t find anything at [http://api.rubyonrails.com/](http://api.rubyonrails.com/)

You could try extending with a method thats build some custom SQL

WHERE MONTH(birthdate);

\_T
