I have query like below
SELECT * FROM programs where startTime between now() and now() + INTERVAL 1 DAY;
Is it possible to write query with time between now and midnight of today in MySQL?
Something like below
SELECT * FROM programs where startTime between now() and now() + midnight 12;
select *from yourTableName where yourColumnName between 'yourStartingDate' and curdate().
Syntax and Parameters: The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.
Current_date() will only give you the date. now() give you the datetime when the statement,procedure etc... started. sysdate() give you the current datetime.
CURRENT_TIME() function in MySQL is used to check the current time. It returns the current time as a value in 'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context.
I suggest you always use clopen (closed-open) intervals for dates and times comparisons. BETWEEN
means closed intervals (from both sides). A very good explanation is in @Aaron Bertrand's blog post: What do BETWEEN and the devil have in common?. Here's a way to write the query:
SELECT *
FROM programs
WHERE startTime >= NOW()
AND startTime < CURRENT_DATE() + INTERVAL 1 DAY ;
SELECT * FROM programs where startTime between now() and CURRENT_DATE() + INTERVAL 1 DAY;
Current date returns the beginning of the day, then we add 1 day to get to the end of it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With