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
Related
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.
I'm trying to subtract now() - the created datetime field from 30 days to get the days remaining as a datetime field, mysql gives me an error for this sort of thing.
SELECT id, created, INTERVAL 30 DAY - CURRENT_DATE - created as timeleft FROM tablename
Use the Date add function to subtract 30 days like this.
SELECT DATE_ADD(current_date, interval -30 day);
Return the difference in days between two date values:
SELECT DATEDIFF(current_date, "2017-06-15");
You can use the combination of these functions to achieve the desired result.
In mysql you should translate the datetime to unix_timestamp to calculate the difference between the two days
SELECT id, created, 30 - (unix_timestamp(CURRENT_DATE) - unix_timestamp(created )) / 3600/24 as timeleft FROM tablename
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'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;
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'