tags:

views:

67

answers:

4

I have a field with type= date in MySQL DB.

It store dates in YY-m-d format.

How can I write a query which will get the rows where day is equal to some value, for example 1?
I need to get only first days of months (2009-11-01, 2009-12-01, 2010-01-01...)

Thanks

+11  A: 

Use the DAYOFMONTH function in the query.

SELECT <columns> FROM <table> WHERE DAYOFMONTH(<the-date-column>) = 1
KennyTM
+2  A: 

you can use DATE_FORMAT method also

SELECT <columns> FROM <table> WHERE DATE_FORMAT(date, "%d") = '1'
Salil
A: 

Another approach would be using like operator:

select <columns> from <table> where <the-date-column> like  '%-01'
YoK
That wouldn't work... day() (synonym for dayofmonth) returns just the day, so the '-01' would never match, I'm afraid.
kander
that was my bad... I have corrected it.I completely agree DAYOFMONTH is best in this case, I have just provided alternative approach.
YoK
Removed my downvote after the correction - alternatives are always good to know, as long as it's been made clear that it's not the optimal solution.
kander
A: 

DAYOFMONTH is the way to go.

NChase