I am trying to create a query to get the records for last two days. In my table there is a field called dates. Values are as below:
05-08-2018 08:05:22
05-08-2018 10:15:42
dd-mm-yyyy hh:ii:ss
I have created the query.
SELECT id,title,description, dates
FROM post_feed where `dates` BETWEEN CURDATE() - INTERVAL 1 DAY AND CURDATE()
ORDER BY dates DESC LIMIT 100
When I run the query it return 0 records. It looks like issue with date format.
Seems like your dates are stored as varchar. You must convert them to date (e.g. by using STR_TO_DATE) before you can perform any comparison.
Assuming for example that today is Aug-05 and you want results for Aug-04 and 05 (inclusive):
SELECT id, title, description, dates
FROM post_feed
WHERE STR_TO_DATE(dates, '%d-%m-%Y') BETWEEN CURRENT_DATE - INTERVAL 1 DAY AND CURRENT_DATE
ORDER BY dates DESC
LIMIT 100
SQL Fiddle
Try this WHERE clause:
WHERE STR_TO_DATE(dates, '%d-%m-%Y %H:%i:%s') BETWEEN DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND CURDATE()
Your second date is before your first date. Put greater date at first place & put lesser date at second place.
SELECT id,title,description, dates
FROM post_feed where CAST(`dates`as date) BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 DAY
ORDER BY dates DESC LIMIT 100
Fix your data! Your query doesn't work because the "date" value are stored as text/varchar. This is easily fixed:
update post_feed
set dates = str_to_date(date, '%Y-%m-%d %H:%i:%s')
alter table post_feed
modify column datetime;
Voila! Your queries will now work.
Related
I have database table with different records and they all have timestamp with them.
When I want to get a certain month (for example April) records is use following query:
SELECT *
FROM `water`
WHERE timestamp >= DATE_FORMAT('2020-04-01', '%Y-%m-%d')
AND timestamp <= DATE_FORMAT('2020-04-30', '%Y-%m-%d')
AND watercar='JV03'
ORDER by timestamp DESC
It will return me records which timestamp is between 01.04.2020-29.04.2020 but it misses the last day of april 30.04.2020 record.
I also tried >= <= and between operators, same issue although the record does exist.
What am I missing?
DB Fiddle: https://www.db-fiddle.com/f/nWFFZmUt7FM17c98DXRRQw/0
Update your query to this:
SELECT *
FROM `water`
WHERE timestamp between DATE_FORMAT('2020-04-01', '%Y-%m-%d 00:00:00') AND DATE_FORMAT('2020-04-30', '%Y-%m-%d 23:59:59') AND watercar='JV03'
ORDER by timestamp DESC
or
SELECT *
FROM `water`
WHERE DATE(timestamp) between DATE_FORMAT('2020-04-01', '%Y-%m-%d') AND DATE_FORMAT('2020-04-30', '%Y-%m-%d') AND watercar='JV03'
ORDER by timestamp DESC
First, there should be no need to use date_format). MySQL should understand dates in the YYYY-MM-DD format.
Second, do not use between with date/time values. Instead, to get everything in April, use:
where timestamp >= date('2020-04-01') and
timestamp < date('2020-05-01')
This formulation works both when the column as a time component and when it does not. So, I recommend it in all situation.
If you want to pass in the end date as a parameter, you can use:
where timestamp >= :start_dt and
timestamp < :end_dt + interval 1 day
I have a table called barcode_log, and these are all the datas from the table.
And now if I run this query
SELECT * FROM `barcode_log` WHERE barcode_log.assign_time BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
I get this result
But it should return all the rows as all the data is within this month only. And assign_time field is stored as datetime. Any idea what i am doing wrong??
You are ignoring the time part (hh:mm:ss).
If the end day is set to the end timestamp of the current date then you can get the data of current day's too.
BETWEEN is inclusive
SELECT
*
FROM
`barcode_log`
WHERE
barcode_log.assign_time BETWEEN DATE_SUB(
CURRENT_DATE,
INTERVAL 30 DAY
)
AND TIMESTAMP(CONCAT(CURDATE(),' ','23:59:59'));
While the accepted answer works, there is a simpler solution. Just take the date part of the datetime column:
SELECT
*
FROM
`barcode_log`
WHERE
DATE(barcode_log.assign_time)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
There is another way around: CAST() on barcode_log.assign_time field.
SELECT *
FROM `barcode_log`
WHERE CAST(barcode_log.assign_time AS DATE)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
This excludes time from comparison and works fine for your purpose.
I am using a SQL query like:
SELECT * FROM game_list
WHERE start_date <= DATE(NOW()) AND end_date >= DATE(NOW())
ORDER BY game_id DESC;
Now, what time will this actually start and end? I mean I know on what date but what will the time be, is it midnight, 12 am, pm or what?
If i understand correctly you want to show a game only if current time is between start time and end time,in that case what you need is actually:
SELECT * FROM game_list
WHERE DATE(NOW()) >= start_date AND DATE(NOW()) <= end_date
ORDER BY game_id DESC;
This way it should work properly
The only problem i could see is if you don't format your start_date and end_date correctly.
If your value is full time stamp,you should simply use NOW() or CURRENT_TIMESTAMP directly as it contains both date AND time
If your start_date is year month day eg: 2012-05-12 you should use CURDATE()
If your value is simply a day number,like 1...2...3...4..etc you should use DAY()
I would doublecheck what start_date returns and decide accordingly,for reference i would take a look here date and time in mysql
If you are asking how the DATE function works: it simply extracts the date part of a datetime.
see http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31
This represents simply that day. Considered as datetime again it would become midnight(2003-12-31 00:00:00)
I think you want something like following
SELECT * FROM game_list
WHERE start_date <= DATE(NOW()) AND end_date >= DATE_SUB(NOW(), INTERVAL 1 DAY)
ORDER BY game_id DESC;
Above query fetch the records which starts before today and having end date greater than tomorrow's date
I am using MySql database. and I am executing query like this:
select *
from Request
where
DATE_FORMAT(created_On,'%e/%m/%Y') between DATE_FORMAT(CURDATE() - 30, '%e/%m/%Y') and DATE_FORMAT(CURDATE(),'%e/%m/%Y')
it will return blank, means no records in result.
but if I write like this
select *
from Request
where
DATE_FORMAT(created_On,'%e/%m/%Y') between DATE_FORMAT(CURDATE() - 27, '%e/%m/%Y') and DATE_FORMAT(CURDATE(),'%e/%m/%Y')
It will fetch 100 hundred rows.
I want to ask that why i am subtract 30 days from current Date?
Assuming your date columns are proper DATETIME or DATE fields, you should not use DATE_FORMAT(). Use DATE_SUB() to subtract 30 days:
If created_On is DATETIME, use DATE() to truncate off the time portion:
SELECT *
from Request
where
DATE(created_On) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
If created_On is DATE, just use:
SELECT *
from Request
where
created_On BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()
You can also use the TO_DAYS function on the dates which allows you to compare dates and datetime fields:
SELECT * from Request
where TO_DAYS(created_On) BETWEEN TO_DAYS(DATE_SUB(CURDATE(), INTERVAL 30 DAY)) AND TO_DAYS(CURDATE())
Can you at least first post a sample of your data and Identify created_on first, so that we could establish a proper analysis of your question? Because to be honest when you are using between the first assumption would be created_on would be of type date, but then again the date_format function transform your date into a string so using between with a string as a value will have a different result than using between with a date
How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'