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

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:

Related

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

MySQL Select rows <= now(), using separated time fields

I have a table 't' with date(yyyy-mm-dd), hour(1-12), minute(00-59), ampm(a/p), and timezone(pst/est) fields.
How can I select the rows that are <= now()? (ie. already happened)
Thank you for your suggestions!
edit: this does it without attention to the hour/minute/ap/tz fields:
SELECT * FROM t.date WHERE date <= now()
Here's one way to do it - combine all your seconds, minutes, etc into a date and compare to NOW(), making sure you do the comparison in the same time-zone. (Untested):
SELECT *
FROM t
LEFT JOIN y ON t.constant=y.constant
WHERE CONVERT_TZ(STR_TO_DATE(CONCAT(date,' ',hour,':',minute,' 'ampm),
'%Y-%m-%d %l:%i %p' ),
timezone,"SYSTEM") < NOW();
If your hour is 01 - 12 not 1-12 then use %h instead of %l in the STR_TO_DATE.
The STR_TO_DATE tries to stick your date and time columns together and convert them into a date.
The CONVERT_TZ(...,timezone,"SYSTEM") converts this date from whatever timezone is specified in the timezone column to system time.
This is then compared to NOW(), which is always in system time.
As an aside, perhaps you should make a single column date using MySQL's date datatype, as it's a lot easier to do arithmetic on that!
For reference, here is a summary of very useful mysql date functions where you can read up on those featuring in this answer.
Good luck!
SELECT * FROM t
WHERE `date`<=DATE_SUB(curdate(), INTERVAL 1 DAY)
OR (
`date`<=DATE_ADD(curdate(), INTERVAL 1 DAY)
AND
CONVERT_TZ(CAST(CONCAT(`date`,' ',IF(`hour`=12 AND ampm='a',0,if(ampm='a',`hour`,`hour`+12)),':',`minute`,':00') AS DATETIME),'GMT',`timezone`)<=NOW()
)
Rationale for date<=DATE_[ADD|SUB](curdate(), INTERVAL 1 DAY):
The fancy conversion is quite an expensive operation, so we don't want it to run on the complete table. This is why we pre-select against an UNCHANGED date field (possibly using an index). In no timezone can an event being more than a day in current timezone's past be in the future, and in no timezone can an event more than a day in the curent timezone's future be in the past.

Where current timestamp between two unix timestamps SQL query

Im trying to make a query where time() is between date_start and date_end
i tried:
date_start > CURRENT_TIMESTAMP AND date_end < CURRENT_TIMESTAMP
and
WHERE CURRENT_TIMESTAMP BETWEEN date_start AND date_end
Still it does not workout for me. Tried CURRENT_TIMESTAMP() too.
Mysql
id item_id percentage rabatcode date_start date_end
9 6 50 XXY-GGS-82 1323817200 1324422000
Compare NOW() against the values via FROM_UNIXTIME():
WHERE NOW() BETWEEN FROM_UNIXTIME(date_start) AND FROM_UNIXTIME(date_end)
Ideally, if it is possible for you to modify your database structure, it is usually much better to store dates as MySQL's native DATETIME type, owing to the fact that MySQL's date processing functions work with the date types without conversion.
You mention in the title of your question that you want to check when the current time is between to UNIX timestamps, so you need to use the UNIX_TIMESTAMP() function:
WHERE UNIX_TIMESTAMP() BETWEEN date_start AND date_end
NOW() returns a string (from the manual: '2007-12-15 23:50:26') so it won't work for your purposes.
The problem is you cannot use CURRENT_TIMESTAMP twice. You should look into converting your dates to a different format and comparing them to NOW() (if they cannot be compared to NOW() already.

MySQL: Curdate() vs Now()

What is difference between MySQL Curdate() and Now()?
For questions like this, it is always worth taking a look in the manual first. Date and time functions in the mySQL manual
CURDATE() returns the DATE part of the current time. Manual on CURDATE()
NOW() returns the date and time portions as a timestamp in various formats, depending on how it was requested. Manual on NOW().
Just for the fun of it:
CURDATE() = DATE(NOW())
Or
NOW() = CONCAT(CURDATE(), ' ', CURTIME())
CURDATE() will give current date while NOW() will give full date time.
Run the queries, and you will find out whats the difference between them.
SELECT NOW(); -- You will get 2010-12-09 17:10:18
SELECT CURDATE(); -- You will get 2010-12-09
Actually MySQL provide a lot of easy to use function in daily life without more effort from user side-
NOW() it produce date and time both in current scenario whereas
CURDATE() produce date only, CURTIME() display time only, we can use one of them according to our need with CAST or merge other calculation it, MySQL rich in these type of function.
NOTE:- You can see the difference using query select NOW() as NOWDATETIME, CURDATE() as NOWDATE, CURTIME() as NOWTIME ;

Comparing Dates in yyyy-mm-dd format

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.