How to fetch rows between two values in a column MySQL? - php

I have saved dates in my MySQL table as VARCHAR format.
I want to fetch rows between two dates.
SELECT * FROM orders WHERE date BETWEEN '01-02-2017' AND '28-02-2017'
This one didn't work. Is there any way to do that?

Try This..
and store date data type as a date
select Date from emp_tbl where eid = 1 and Date >= '2016/03/31' and Date <= '2017/03/31'

Related

Select items between two dates, varchar format

I have a table who contains a field 'date' which is a date in varchar format ("2016-07-30"). I would like to select all items which are between the date of today and the date of today +1 month. I already done the variable who contains the two dates (in date format) but I don't know how to select items between these two dates because the field of my table is in varchar and not in date format.
Thank you so much for your help.
Try something like this:
select *
from yourtable
where str_to_date(datecol, '%Y-%m-%d') between now() and date_add(now(), interval 1 month)
But the right thing you should do is alter your column type to date.

How to select data from table according to dates in MySQL?

I have a table called tasks. In the index file all the tasks are being listed. There is a column name date in tasks. The date is saved in YYYY-MM-DD format. I want to get all the rows whose date is 2016-numbers-from-0-to-12-numbers-from-0-to-30.
example date is:
2016-12-1
You can simply filter the records by specifying date range.
SELECT *
FROM tasks
WHERE date BETWEEN '2016-01-01' AND '2016-12-31'
ORDER BY date DESC
Update
To get records from current date:
SELECT *
FROM tasks
WHERE date BETWEEN curdate() AND '2016-12-31'
ORDER BY date DESC

php and mysql use the date range can not show

I had the question for the mysql date between select. In the mysql table, the field is varchar. The date range is '21-01-2013' and '31-01-2013', it can show the records, but the date range is '21-01-2013' and '20-02-2013', it cannot show the records.
"SELECT * from away_from_office where (awaydatefrom between '21-01-2013' and '31-01-2013') ";
you should convert it to date first using STR_TO_DATE, eg.
SELECT *
from away_from_office
where awaydatefrom between STR_TO_DATE('21-01-2013', '%d-%m-%Y') and
STR_TO_DATE('31-01-2013', '%d-%m-%Y')
if the column has the same format with the one you've shown, convert it also
WHERE STR_TO_DATE(awaydatefrom, '%d-%m-%Y') BETWEEN
if you have a chance to alter the table, or you're working with sample records, alter your table by changing the data type of the column to DATE.
Change column datatype to DATE
http://dev.mysql.com/doc/refman/5.1/en/datetime.html

mySQL selecting records due within the next month

I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND  DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.

Retrieve data for a time interval from a DATETIME Column MySQL /PHP

Im pulling information from a database and ive got the query working fine except i'd like to only select conent from a certain date range. Each row has a field with the created date stored as a DATETIME field. What is the basic syntax?
SELECT fields
FROM table
WHERE date BETWEEN '$startDate' AND '$endDate'
Dates in MySQL are in YYYY-MM-DD format, so the actual query would look like:
SELECT fields
FROM table
WHERE date BETWEEN '2010-10-01' AND '2010-09-25'
WHERE `date_field` BETWEEN '2010-09-21 12:13:14' AND '2010-09-28 12:13:14'
Here's the link
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
select * from table where datetime between DATE1 and DATE2
SELECT * FROM table
WHERE DateTime
BETWEEN time1 AND time2

Categories