date between in mysql not correctly working
NickName:amit munde Ask DateTime:2017-10-01T19:50:56

date between in mysql not correctly working

when I fetch data from table "like date from 01/09/2017 to 30/09/2017" then it's okey..

BUT When I am trying to fetch data from date 01/09/2017 to 01/10/2017 then its only showing the data of DATE 01/10/2017(not previous month data i.e 01/09/2017)

I am using MySQL Database.

SELECT * FROM `tablename` where date between '01/09/2017' AND '01/10/2017'

Copyright Notice:Content Author:「amit munde」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46512102/date-between-in-mysql-not-correctly-working

Answers
Chrysovalantis Koutsoumpos 2017-10-01T11:55:45

If you are saving the value as DATE format it should work. If not (you are saving the data as VARCHAR you can convert it to date and get the correct results.\n\nSTR_TO_DATE('01/09/2017', '%m/%d/%Y')\n",


GrahamTheDevRel 2017-10-01T12:03:52

You need to store dates as DATE type not VARCHAR or TEXT.\n\nAlso DB dates are in the format YYYY-MM-DD usually so you will need to adjust your query accordingly.\n\nDue to speed trying to use STR_TO_DATE is a terrible idea, better to convert once and then use MySQL as intended.\n\nBackup your data first and then I (think) the following will work\n\nBEGIN;\nALTER TABLE `tablename` \nADD COLUMN `new_date` DATE;\n\nUPDATE `tablename` \nSET `new_date` = STR_TO_DATE(`date`, '%d/%m/%Y');\n\nALTER TABLE `tablename` \nDROP COLUMN `date`;\n\nALTER TABLE `tablename` \nCHANGE COLUMN `new_date` `date` DATE;\nCOMMIT;\n\n\nStep By Step -\n\n\nAdd an extra column to store the data temporarily\nUpdate the table and copy the current date column value (formatted\nDB friendly date) into the new temp column.\nRemove the old column\nChange the column name to the previous name so all existing queries work.\n\n\nThen your query is as simple as\n\nSELECT * FROM `tablename` where date between '2017-09-01' AND '2017-10-01'\n",


More about “date between in mysql not correctly working” related questions

date between in mysql not correctly working

when I fetch data from table "like date from 01/09/2017 to 30/09/2017" then it's okey.. BUT When I am trying to fetch data from date 01/09/2017 to 01/10/2017 then its only showing the data of DATE...

Show Detail

Fail to select the FROM date correctly in MYSQL statement

$from = "2017-7-1"; $to = "2017-8-25"; I don't know why i cant seems to select the from date in the HTML5 date picker correctly, for instance when i pick 2017-7-1, the result page wont show the ...

Show Detail

Check mysql if date is between dates

i'm learning php at the moment and i wanted to make a car hire system to challenge myself a bit. Now i am stuck with a validation question, i got 5 diffrent cars and want to check if the car is

Show Detail

Mysql between query not working

I am using mysql. I want to display records between two dates. I searched through web and found that mysql's date format is yyyy/mm/dd. so I am writing query as follows, select * from

Show Detail

Working with mysql date range

I am attempting to retrieve records from a mysql database based on a specific date range using the following query: $query = "SELECT * FROM data WHERE" . "date between '11/02/13' and '1...

Show Detail

date range in Mysql (between)

I want to get all the values in the database where date is between two given parameters. Following is my code. Here Date is $date1' and '$date2 are the given parameters. $result1 = mysql_query("SE...

Show Detail

Between date in Mysql not working properly

I want to filter some items based on two different dates using MySQL. In my database I store data like 2017-03-28 10:55:10. But I need only the date part, not the time so I used the DATE() function:

Show Detail

MYSQL Date range query not working correctly

I have a problem getting certain data from my database by querying a date range. In my database i have a DATE type field within the format YYYY-MM-DD. I want to get all data within a date range of ...

Show Detail

Getting rows between date range in mysql

I am having a serious issue I can not figure out. I am trying to return rows from MySQL database that have a Start date between two given dates and an End date as well. Here is my php code to query...

Show Detail

ANSI 92 Date Difference not working in MySQL

I'm, trying to calculate the number of days between two dates using ANSI SQL standard. But I'm missing something as this statement returns NULL in MySQL. SELECT EXTRACT(DAY FROM DATE('2009-01-25'...

Show Detail