Comparing Dates in yyyy-mm-dd format - mysql

There's a query I'm working on that has
... WHERE start_date >= DATE(NOW()) AND end_date >= DATE(NOW())
start_date/end_date are in the date format: yyyy-mm-dd
What kinds of results should be expected from this kind of query?

Both start_date and end_date is greater than or equal to today's date.
Instead of using DATE(now()) you can use CURRENT_DATE().

What kinds of results should be expected from this kind of query?
If start_date and end_date are proper mySQL DATE fields, it should work just fine and return all entries where start_date AND end_date are today's date, or a date in the future.
If they are varchar fields, you may want to consider converting the columns into DATE fields - I assume the values are going to remain but make a backup of course - or casting them as DATE fields (slow!):
WHERE CONVERT(start_date, DATE) => DATE(NOW()) AND CONVERT(end_date, DATE) => DATE(NOW())

you will be comparing dates at the date (i.e. time not considered) level of granularity. i.e. all rows where the day is on or later than today (NOW()), assuming that start_date and end_date are date columns.

This should work fine, as you can use NOW() within a date comparision.
However, you don't need to wrap NOW() within DATE - it'll work as-is as long as you're using one of the time related (DATE, DATETIME, TIMESTAMP, etc.) field types.
As such, you should just be able to use...
WHERE start_date >= NOW() AND end_date >= NOW()

That would work, provided that you surrounded start_date and end_date with single quotes. It would give you all the records where the start date and end date are in the future.

Related

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'

What would be the difference between NOW() and DATE(NOW())?

What is the difference between NOW() and DATE(NOW()) in terms of performance, results and precision? How will MySQL understand those 2 functions?
I was wondering what is the best between
WHERE the_date < NOW()
and
WHERE the_date < DATE(NOW())
NOW() returns current date/time. DATE(NOW()) returns the date part. If you want to know only the date, use CURRDATE().
You are looking at two different comparisons.
Since the_date contains a time of day, the expression the_date < NOW() will take into account the time of day at the moment you execute the SQL statement, which could make a difference if the YYYY-MM-DD in the_date is the same as the current datetime.
In the case of the_date < DATE(NOW()), you are comparing the_date to YYYY-MM-DD 00:00:00000, so the current time of day from NOW() is ignored and set to midnight.
What is more important here is what you need to do and what results you are expecting.
The DateTime.Now property returns the current date and time, for example 2011-07-01 10:09.45310.
The DateTime.Today property returns the current date with the time components set to zero, for example 2011-07-01 00:00.00000.
The DateTime.Today property actually returns DateTime.Now.Date:

What time does a SQL query like this one start and end?

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

Checking conditions (date, time) with MySQL

I need to check if the current time is within a certain range of fields within the DB.
For example in the DB I would store a start time, end time, start date, and end date.
I then need to check if the current date is within the rage of values returned by the query.
I have attempted:
WHERE start_date <= $currdate
AND end_date >= $currdate
AND start_time <= $currtime
AND end_time >= $currtime
Which works great, until we get a situation where the dates overlap two days, in which case the query returns NULL as end_time is obviously LESS than the current time.
I guess I need a way to combine start_date and start_time with end_date and end_time?
Have you tried timestamp()?
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestamp
Perhaps can do something like
WHERE timestamp(start_date, start_time) <= timestamp(currdate, currtime)
AND timestamp(end_date, end_time) >= timestamp(currdate, currtime)
This is assuming, of course, that your start/end date and time are related. Not fully sure what the business logic is behind separating those two fields...

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

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