15

I have a mysql DB that has a TIMESTAMP field titled date. How can I select all fields where the month is the current month?

Thanks in advance!

rodzilla
  • 335
  • 2
  • 4
  • 13

12 Answers12

33

UPDATE

A much better index-friendly way to query your data for a range of dates

SELECT id, FROM_UNIXTIME(timestampfield) timestamp 
  FROM table1
 WHERE timestampfield >= UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH)
   AND timestampfield <  UNIX_TIMESTAMP(LAST_DAY(CURDATE()) + INTERVAL 1 DAY);

Note: You don't apply any function to your column data, but rather do all necessary calculations on the right side of the conditions (which are constants and are evaluated only once post-execution). This way you allow MySQL to benefit from index(es) that you might have on the timestampfield column.

Original answer:

SELECT id, FROM_UNIXTIME(timestampfield) timestamp 
  FROM table1
 WHERE MONTH(FROM_UNIXTIME(timestampfield)) = MONTH(CURDATE())
   AND YEAR(FROM_UNIXTIME(timestampfield)) = YEAR(CURDATE())

Note: Although this query produces the correct results it effectively invalidates the proper usage of the index(es) that you might have on the timestampfield column (meaning MySQL will be forced to perform a fullscan)

Here is SQLFiddle demo

Serg
  • 2,346
  • 3
  • 29
  • 38
peterm
  • 91,357
  • 15
  • 148
  • 157
  • There's a [YEAR function](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_year) for that, Mr Pierce. – hd1 May 11 '13 at 18:50
  • i know :> wouldn't wanna pick up May 2011 and 2012 would ya – Drew May 11 '13 at 18:51
  • Strange, but it's not working for me, neither solution. I have a table filled with dates for this month (timestamp column), but it's not returning anything. If I remove the last line in the index-friendly solution, I get everything. Any thoughts? – zack_falcon Jan 21 '21 at 09:39
  • @zack_falcon I know this is late, but I came across the same issue as you. Just remove the 'UNIX_' before each 'TIMESTAMP' and it will work as expected. – adherb Mar 01 '22 at 21:55
6

Use this query may this help you,

Query = "SELECT * FROM <table_name> WHERE MONTH(date_entered) = MONTH(CURDATE())";
i'm PosSible
  • 1,373
  • 2
  • 11
  • 30
  • 2
    This will get any row with matching month value even if its year is different. – Olcay Ertaş Apr 25 '18 at 11:57
  • 2
    yes, it will get you the records from diffs years, if you want to specify the current year just add an operator and condition for the year, like this: SELECT * FROM WHERE MONTH(created_at) = MONTH(CURDATE()) AND YEAR(created_at) = YEAR(CURDATE()) – codeNeverDie Jul 25 '19 at 11:52
6

In my opinion, the following is more readable than the accepted answer...

SELECT id, FROM_UNIXTIME(timestampfield) timestamp 
FROM table1
WHERE timestampfield >= DATE_FORMAT(NOW(), '%Y-%m-01')

Note: This would select any records from the next month as well. That usually doesn't matter, because none have been created.

musicin3d
  • 1,028
  • 1
  • 12
  • 22
3

If you want indexes to be used, don't apply any function to the column:

SELECT * 
FROM tableX
WHERE `date` >= UNIX_TIMESTAMP((LAST_DAY(NOW())+INTERVAL 1 DAY)-INTERVAL 1 MONTH)
  AND `date` <  UNIX_TIMESTAMP(LAST_DAY(NOW())+INTERVAL 1 DAY) ;

The functions used can be found in MySQL docs: Date and Time functions

ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
2

try this

SELECT * FROM table WHERE month(data) = EXTRACT(month FROM (NOW()))
offboard
  • 269
  • 1
  • 12
  • 1
    Above query will compare only month so you may get previous year's month record.. I think correct query is "==== SELECT * FROM table WHERE month(data) = EXTRACT(month FROM (NOW())) AND year(data) = EXTRACT(year FROM (NOW())) =====" – Paresh Thummar Jan 05 '15 at 06:23
2
SELECT 'data of your choice '
FROM 'your table'
WHERE
MONTH'datecolumn'=MONTH(CURRENT_DATE )

replace text in ' ' with appropriate from your database

methode
  • 5,348
  • 2
  • 31
  • 42
Foxx
  • 21
  • 1
1
SELECT [columns] 
FROM [the_table] 
WHERE MONTH([date_column]) = MONTH(CURDATE())

Replace the text between [] (including the []) with your data.

Farhan
  • 33
  • 7
0

The query below can benefit from the index and no functions applied to the timestamp field for where clause evaluation.

SELECT * 
FROM TableName 
WHERE TimestampField >= 
           (CURDATE() - INTERVAL (DAY(CURDATE())-1) DAY) 
      AND TimestampField <  
           LAST_DAY(CURDATE()) + INTERVAL 1 DAY;

If your timestamp field is time part is truncated, go for this one,

SELECT * 
FROM TableName 
WHERE TimestampField BETWEEN 
           (CURDATE() - INTERVAL (DAY(CURDATE())-1) DAY) 
         AND 
           LAST_DAY(CURDATE());
MikA
  • 5,184
  • 5
  • 33
  • 42
0

As of 2020, you can use BETWEEN to handle the query from the very beginning.

SELECT *
FROM [TABLE]
WHERE [DATE_FIELD] 
BETWEEN 
CAST('2020-30-01' AS DATE) AND CAST('2020-10-31' AS DATE);

I know is not the most "automatic" way, but from a SQL perspective it is very friendly and straightforward.

Source https://www.techonthenet.com/mysql/between.php

gvelasquez85
  • 511
  • 4
  • 3
0

Try this one it will work better because of the range. You don't need to calculate month and year for every row. It will slow the process. User range for better performance.

SELECT * FROM table WHERE columnName between DATE_FORMAT(current_date() ,'%Y-%m-01') and current_date();
Sandeep Kumar
  • 1,172
  • 1
  • 9
  • 22
0
SELECT 
    *
FROM
    tableName
WHERE
    EXTRACT(YEAR_MONTH FROM columnName) = EXTRACT(YEAR_MONTH FROM CURDATE())
Nurullah
  • 82
  • 3
-3

I think in MySQL here is the simplest method which i have tried and works well, you want to select rows where timestampfield is in this month.

SELECT * FROM your_table 
WHERE MONTH(timestampfield)=MONTH(CURRENT_DATE()) AND
YEAR(timestampfield)=YEAR(CURRENT_DATE());

the above will return all records that the timestampfield is this month in MySQL

indago
  • 2,041
  • 3
  • 29
  • 48
  • this will go back one month instead of selecting the current month. So you'll get half the previous month. – John Oct 20 '17 at 01:07