152

I have a table in the MySQL database that is set up with DATETIME. I need to SELECT in this table only by DATE and excluding the time.

How do I SELECT in this table by only date and bypassing the time, even if that specific column is set to DATETIME?


Example

Now it is: 2012-01-23 09:24:41

I need to do a SELECT only for this: 2012-01-23

Community
  • 1
  • 1
DiegoP.
  • 45,177
  • 34
  • 89
  • 107
  • possible duplicate of [MySQL date comparison issue](http://stackoverflow.com/questions/5229063/mysql-date-comparison-issue) – Marcus Adams Jan 27 '12 at 14:02

17 Answers17

225

SELECT DATE(ColumnName) FROM tablename;

More on MySQL DATE() function.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
  • I am trying to do this but it does not work $query="SELECT * FROM Profiles WHERE date(DateReg='".$date."')"; – DiegoP. Jan 27 '12 at 10:50
  • $query="SELECT * FROM Profiles WHERE date(DateReg) ='".$date."'"; This should work. Make sure that both dates are of same format, If not format the $date before using in 'where" clause. – Clain Dsilva Nov 25 '13 at 11:49
  • 1
    I think you need to be carefull with that because it will not use an index on the ColumnName – d0x Jul 01 '15 at 09:03
  • Donot forget to add the Grave accent symbol ` before and after the column name . In some MySql version it may show the current date. – user2092317 Jul 28 '15 at 14:49
62

you can use date_format

select DATE_FORMAT(date,'%y-%m-%d') from tablename

for time zone

sql2 = "SELECT DATE_FORMAT(CONVERT_TZ(CURDATE(),'US/Central','Asia/Karachi'),'%Y-%m-%d');"
Yann Chabot
  • 4,789
  • 3
  • 39
  • 56
sushant goel
  • 821
  • 6
  • 8
  • 1
    This works perfect because now I can select data from certain days by just having date format '%d'. – mjwrazor Mar 21 '16 at 17:31
  • **select DATE_FORMAT(date,'%Y-%m-%d') from tablename** small "y" is not returning year use capital "Y" instead – Nirav Joshi Feb 09 '18 at 18:25
21

Try to use
for today:

SELECT * FROM `tbl_name` where DATE(column_name) = CURDATE()


for selected date:

SELECT * FROM `tbl_name` where DATE(column_name) = DATE('2016-01-14')
Mohammed Aly
  • 211
  • 2
  • 3
  • This is so simple it makes me feel embarrassed. I used to check if the datetime value was between yesterday and tomorrow. But all I need to do is use the DATE() function. Why didn't I think of this years ago. Thanks for posting your simple solution. – ideaztech Jul 07 '22 at 13:16
17

You can use select DATE(time) from appointment_details for date only

or

You can use select TIME(time) from appointment_details for time only

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
user1061865
  • 181
  • 1
  • 6
12

In MYSQL we have function called DATE_FORMAT(date,format). In your case your select statement will become like this:-

SELECT DATE_FORMAT(dateTimeFieldName,"%a%m%Y") as dateFieldName FROM table_name

For more information about Mysql DATE and TIME functions click here.

Santosh D.
  • 537
  • 6
  • 19
9

Please try this answer.

SELECT * FROM `Yourtable` WHERE date(`dateField`) = '2018-09-25'
7

Simply You can do

SELECT DATE(date_field) AS date_field FROM table_name
krish kim
  • 192
  • 4
  • 9
6

I tried doing a SELECT DATE(ColumnName), however this does not work for TIMESTAMP columns because they are stored in UTC and the UTC date is used instead of converting to the local date. I needed to select rows that were on a specific date in my time zone, so combining my answer to this other question with Balaswamy Vaddeman's answer to this question, this is what I did:

If you are storing dates as DATETIME

Just do SELECT DATE(ColumnName)

If you are storing dates as TIMESTAMP

Load the time zone data into MySQL if you haven't done so already. For Windows servers see the previous link. For Linux, FreeBSD, Solaris, and OS X servers you would do:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Then format your query like this:

SELECT DATE(CONVERT_TZ(`ColumnName`, 'UTC', 'America/New_York'))

You can also put this in the WHERE part of the query like this (but note that indexes on that column will not work):

SELECT * FROM tableName
WHERE DATE(CONVERT_TZ(`ColumnName`, 'UTC', 'America/New_York')) >= '2015-02-04'

(Obviously substitute America/New_York for your local time zone.)


The only exception to this is if your local time zone is GMT and you don't do daylight savings because your local time is the same as UTC.

Community
  • 1
  • 1
Mike
  • 23,542
  • 14
  • 76
  • 87
3

Yo can try this:

SELECT CURDATE();

If you check the following:

SELECT NOW(); SELECT DATE(NOW()); SELECT DATE_FORMAT(NOW(),'%Y-%m-%d');

You can see that it takes a long time.

plamut
  • 3,085
  • 10
  • 29
  • 40
aymsoft
  • 31
  • 1
3

Try SELECT * FROM Profiles WHERE date(DateReg)=$date where $date is in yyyy-mm-dd

Alternatively SELECT * FROM Profiles WHERE left(DateReg,10)=$date

Cheers

Richie
  • 9,006
  • 5
  • 25
  • 38
2
Select * from table_name where date(datetime)
Gurpreet Dhanoa
  • 131
  • 1
  • 2
2

Use DATE_FORMAT

select DATE_FORMAT(date,'%d') from tablename =>Date only

example:

select DATE_FORMAT(`date_column`,'%d') from `database_name`.`table_name`;
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
Hendry Tanaka
  • 454
  • 3
  • 11
2

you can use date_format

select DATE_FORMAT(date,'%y-%m-%d') from tablename

for time zone

sql2 = "SELECT DATE_FORMAT(CONVERT_TZ(CURDATE(),'US/Central','Asia/Karachi'),'%Y-%m-%d');"

You can use select DATE(time) from appointment_details for date only

or

You can use select TIME(time)  from appointment_details for time only
if time column is on timestamp , you will get date value from that timestamp using this query

SELECT DATE(FROM_UNIXTIME(time)) from table

Tayyeb
  • 127
  • 7
1
SELECT DATE_FORMAT(NOW() - INTERVAL FLOOR(RAND() * 14) DAY,'%Y-%m-%d');

This one can be used to get date in 'yyyy-mm-dd' format.

zaynomer
  • 11
  • 2
1

if time column is on timestamp , you will get date value from that timestamp using this query

SELECT DATE(FROM_UNIXTIME(time)) from table 
Rosmin
  • 21
  • 3
0

In the interest of actually putting a working solution to this question:

SELECT ... WHERE `myDateColumn` >= DATE(DATE_FORMAT(NOW(),'%Y-%m-%d'));

Obviously, you could change the NOW() function to any date or variable you want.

Mo'in Creemers
  • 1,139
  • 8
  • 17
-1

I solve this in my VB app with a simple tiny function (one line). Code taken out of a production app. The function looks like this:

Public Function MySQLDateTimeVar(inDate As Date, inTime As String) As String
Return "'" & inDate.ToString(format:="yyyy'-'MM'-'dd") & " " & inTime & "'"
End Function

Usage: Let's say I have DateTimePicker1 and DateTimePicker2 and the user must define a start date and an end date. No matter if the dates are the same. I need to query a DATETIME field using only the DATE. My query string is easily built like this:

Dim QueryString As String = "Select * From SOMETABLE Where SOMEDATETIMEFIELD BETWEEN " & MySQLDateTimeVar(DateTimePicker1.Value,"00:00:00") & " AND " & MySQLDateTimeVar(DateTimePicker2.Value,"23:59:59")

The function generates the correct MySQL DATETIME syntax for DATETIME fields in the query and the query returns all records on that DATE (or BETWEEN the DATES) correctly.

Ray E
  • 134
  • 1
  • 9