Find results in certain date range - mysql

I am trying to only grab records that fall in a certain date range. The problem is that the timestamp and the date are stored as a string in the same cell. I want to only grab rows with a date that falls betweed 2013-05-01 and 2013-05-03.
date (stored as string)
2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17
mysql
SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03

Try
SELECT *
FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'
SQLFiddle
As #FreshPrinceOfSO absolutely correctly noted no index will be used in that case

SELECT * FROM table1
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y')
BETWEEN '2013-05-01' AND '2013-05-03'

The string is almost valid syntax for a datetime. Thus, an alternative, if perhaps slower method is to replace the 'T' with a space and then cast it to a datetime.
SELECT * FROM table1 WHERE
CAST(REPLACE(`date`, 'T', ' ') AS DATETIME)
BETWEEN '2013-05-01' AND '2013-05-03';

SELECT * FROM table
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')

Related

How to select a date and ignor the time

I made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?
You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)
Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate
GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);
Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1

mysql select with priority of 3 filelds in a query

my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453
If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')

How to select from a DATETIME column using only a date?

I have a DATETIME column on my table which stores when a record was created. I want to select only the records created on a particular date. If I try:
SELECT *
FROM myTable
WHERE postedOn = '2012-06-06'
It returns no rows even though there are many rows in the table with the postedOn set as
2012-06-06 21:42:02, 2012-06-06 07:55:17 , and so forth.
Any ideas?
Use the DATE scalar:
SELECT *
FROM myTable
WHERE date(postedOn) = '2012-06-06'
SELECT *
FROM myTable
WHERE DATE(postedOn) = '2012-06-06'
DATE() returns the date part of a datetime field.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
Create a time range by adding a day to the date:
SELECT *
FROM myTable
WHERE postedOn >= '2012-06-06' and postedOn < '2012-06-07'
This is the most efficient way, as the query can use an index on the field.

How to select only date from a DATETIME field in MySQL?

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
SELECT DATE(ColumnName) FROM tablename;
More on MySQL DATE() function.
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');"
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')
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
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.
Please try this answer.
SELECT * FROM `Yourtable` WHERE date(`dateField`) = '2018-09-25'
Simply You can do
SELECT DATE(date_field) AS date_field FROM table_name
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.
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
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.
Select * from table_name where date(datetime)
Use DATE_FORMAT
select DATE_FORMAT(date,'%d') from tablename =>Date only
example:
select DATE_FORMAT(`date_column`,'%d') from `database_name`.`table_name`;
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
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.
if time column is on timestamp , you will get date value from that timestamp using this query
SELECT DATE(FROM_UNIXTIME(time)) from table
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.
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.

Row count differs when using DATE and DATETIME

As the title says, the row count differs when doing a select using DATE and DATETIME. Please advise.
I'm trying to select rows between 1st and 5th Jan, 2012. The date column datatype is bigint (UNIX timestamp).
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01')*1000 and unix_timestamp('2012-01-05')*1000
If I include the time in HH:MM:SS, the rows returned are correct i.e.
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01 00:00:00')*1000 and unix_timestamp('2012-01-05 23:59:59')*1000
Any input will be much appreciated. Thanks.
'2012-01-05' is actually '2012-01-05 00:00:00' which is not what you're writing in the second select.
I suspect what you mean to do is
select * from table_name
where sample_timestamp >= unix_timestamp('2012-01-01')*1000
and sample_timestamp < unix_timestamp('2012-01-06')*1000
which as a bonus handles leap seconds correctly too :)