221

What's the best way to do following:

SELECT * FROM users WHERE created >= today;

Note: created is a datetime field.

Matt
  • 14,906
  • 27
  • 99
  • 149
n00b
  • 16,088
  • 21
  • 56
  • 72

10 Answers10

400
SELECT * FROM users WHERE created >= CURDATE();

But I think you mean created < today

You can compare datetime with date, for example: SELECT NOW() < CURDATE() gives 0, SELECT NOW() = CURDATE() gives 1.

questionto42
  • 7,175
  • 4
  • 57
  • 90
Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
94
SELECT * FROM myTable WHERE  DATE(myDate) = DATE(NOW())
Rich Steinmetz
  • 1,020
  • 13
  • 28
Edmhs
  • 3,645
  • 27
  • 39
  • 8
    Maybe I'm missing something, but with DATETIME types, the CURDATE() solution doesn't work. This does. – James John McGuire 'Jahmic' Oct 18 '13 at 08:42
  • plus this takes a lot of time as there is overhead of converting DATETIME to date via the DATE() function and then comparing with the where condition. – roopunk Aug 14 '14 at 09:48
  • 1
    Do not use functions on columns, it makes the query to ignore index, causing slow queries. Check my answer for alternative approach https://stackoverflow.com/a/42365426/4311336 – Abdul Rehman Aug 26 '19 at 13:00
  • "equal or greater than today" - how does your answer resolve that? How could it return any row where `myDate` is in the future? – Nico Haase Jun 21 '23 at 12:36
46
SELECT * FROM users WHERE created >= NOW();

if the column is datetime type.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
28

Answer marked is misleading. The question stated is DateTime, but stated what was needed was just CURDATE().

The shortest and correct answer to this is:

SELECT * FROM users WHERE created >= CURRENT_TIMESTAMP;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
JeffMan
  • 412
  • 4
  • 11
18

If 'created' is datetime type

SELECT * FROM users WHERE created < DATE_ADD(CURDATE(), INTERVAL 1 DAY);

CURDATE() means also '2013-05-09 00:00:00'

bart
  • 321
  • 2
  • 9
15

If the column have index and a function is applied on the column then index doesn't work and full table scan occurs, causing really slow query.

Bad Query; This would ignore index on the column date_time

select * from users
where Date(date_time) > '2010-10-10'

To utilize index on column created of type datetime comparing with today/current date, the following method can be used.

Solution for OP:

select * from users
where created > CONCAT(CURDATE(), ' 23:59:59')

Sample to get data for today:

select * from users
where 
    created >= CONCAT(CURDATE(), ' 00:00:00') AND
    created <= CONCAT(CURDATE(), ' 23:59:59')

Or use BETWEEN for short

select * from users 
where created BETWEEN 
      CONCAT(CURDATE(), ' 00:00:00') AND CONCAT(CURDATE(), ' 23:59:59')

Tip: If you have to do a lot of calculation or queries on dates as well as time, then it's very useful to save date and time in separate columns. (Divide & Conquer)

Abdul Rehman
  • 1,662
  • 3
  • 22
  • 36
3
SELECT * FROM users WHERE created >= now()
kapa
  • 77,694
  • 21
  • 158
  • 175
Silx
  • 2,663
  • 20
  • 21
1

The below code worked for me.

declare @Today date

Set @Today=getdate() --date will equal today    

Select *

FROM table_name
WHERE created <= @Today
slavoo
  • 5,798
  • 64
  • 37
  • 39
-2
SELECT * FROM table_name WHERE CONCAT( SUBSTRING(json_date, 11, 4 ) ,  '-', SUBSTRING( json_date, 7, 2 ) ,  '-', SUBSTRING(json_date, 3, 2 ) ) >= NOW();

json_date ["05/11/2011"]

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
vpgodara
  • 329
  • 2
  • 2
-6

you can return all rows and than use php datediff function inside an if statement, although that will put extra load on the server.

if(dateDiff(date("Y/m/d"), $row['date']) <=0 ){    
}else{    
echo " info here";    
}
Pathik Vejani
  • 4,263
  • 8
  • 57
  • 98