1

How would I go about doing a query that returns results of all rows that contain dates for current year and month at the time of query.

Timestamps for each row are formated as such: yyyy-mm-dd

I know it probably has something to do with the date function and that I must somehow set a special parameter to make it spit out like such: yyyy-mm-%%.

Setting days to be wild card character would do the trick but I can't seem to figure it out how to do it.

Here is a link to for quick reference to date-time functions in mysql:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Thanks

OMG Ponies
  • 314,254
  • 77
  • 507
  • 490
payling
  • 2,466
  • 5
  • 33
  • 44

3 Answers3

13

I think EXTRACT is the function you are looking for:

SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM NOW())
Eric Petroelje
  • 58,601
  • 9
  • 123
  • 175
5

you could extract the year and month using a function, but that will not be able to use an index.

if you want scalable performance, you need to do this:

SELECT *
  FROM myTable
 WHERE some_date_column BETWEEN '2009-01-01' AND '2009-01-31'
longneck
  • 11,532
  • 2
  • 35
  • 43
1
select * from someTable where year(myDt) = 2009 and month(myDt) = 9 and day(myDt) = 12
Deenadhayalan Manoharan
  • 5,357
  • 14
  • 29
  • 48
Alexey Sviridov
  • 3,129
  • 28
  • 32