SELECT MySQL rows where today's date is between two DATE columns - mysql

How can I get the rows in a table where today's date is between (inclusive) two DATE columns of that row?
For example, take these two columns of a table:
How could I get the first and second rows on the 10th of April, or the 3rd row on the 25th (inclusive, like I said)?
Any help would be greatly appreciated. Thanks in advance!

You can add a condition as follows
DATE(NOW()) between date1 and date2

You will find a lot of people using between operator, but I prefer using a simple AND operator.
I do that because although the between operator IS inclusive, simple dates (2012-04-10) can be counted as being midnight, and will thus not be inclusive.
So this should work just fine and will always include the boundaries of the date range:
SELECT * FROM table WHERE from_date <= '2012-04-10' AND to_date >= '2012-04-10'

Just use the SQL now() function to compare the date columns like so:
SELECT * from table where now() >= from_date and now() <= to_date

If you have date (not datetime) columns, use CURTIME() or DATE(NOW()), never NOW() as CesarC correct wrote and you can use BETWEEN.
SELECT * FROM table WHERE CURTIME() BETWEEN from_date AND to_date

Related

Get records for last 2 days

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.

How to compare a date when the date is stored as a varchar

The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'

Adding days to a start date to create and ending date

I have a table with a "start_date" column which contains date and a "duration" column with numeric value. I'd like to make a query to have a column "end_date". In Excel, I would simply add the start_date column with the duration column in a end_date column in date format. I don't know how to do it with MySQL.
If I do "SELECT start_date, duration, start_date+duration AS "end_date", MySQL will treat the first two columns as numeral values and I will have absurd values for dates (2013/10/30 + 2 will result in 20131032) in the "end_date" column.
Is there a simple way to do in a MySQL query the same thing that I'm doing in an excel sheet?
Thanks
Use DATE_ADD()
SELECT
start_date,
duration,
DATE_ADD(start_date, INTERVAL duration DAY) AS end_date
I think you are looking for this:
DATE_ADD([Date starting with],INTERVAL [number of days] DAY);
You can also use month and year. Here is the documentation on it.
The answer is in the DATE_ADD(date, INTERVAL expr unit)
MySQL function.
Using it this way :
DATE_ADD(startdate,INTERVAL duration DAY)

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

Return all rows which are same day in MySQL

I store a date in my database as a string like this:
03/08/2013 --> 8th of march
I'm trying to select only the rows that are the same day as the current day:
SELECT * FROM wp_aerezona_booking WHERE DATE_SUB(CURDATE(),INTERVAL 1
DAY) <= STR_TO_DATE(date, '%m/%d/%Y')
The above is what I tried, but it is returning a lot of results and should only return 1.
This should work already:
SELECT * FROM wp_aerezona_booking
WHERE STR_TO_DATE('03/08/2013', '%m/%d/%Y') = CURDATE();
By using the DATE_SUB you are subtracting1 day from the current day. You're not looking at today but yesterday. Also the <= makes you look at yesterday and all days before that.
Then you don't want <=, but you want =. The former will get all results if date is less than or equal to yesterday's date. I'm not sure that you even want the DATE_SUB either.
If you want the same date as today's date then you have to use "=" operator with.
SELECT *
FROM wp_aerezona_booking
WHERE STR_TO_DATE(date, '%m/%d/%Y')= CURDATE()