So I have a column with time type and the entries are saved as 12:15:00, what I'd like to do is get
SELECT * FROM table WHERE time > NOW() however this doesn't work because NOW() returns a datetime not just time. Is there a way to do this?
You can use the time() function or curtime():
SELECT *
FROM table
WHERE time > curtime();
Related
I need to write a filter condition in SQL something of this sort -
select * from table where date > now() - INTERVAL 2 DAY
(Works in MySQL)
But this query fails in H2( Spring Boot Application). Can someone help in formulating the query which will filter date from current time stamp to 2 days before.
Tried different queries - nothing seems to work with H2.
Try DATEADD() function in H2. Alternative to NOW() in H2 is CURRENT_TIMESTAMP
select * from table where date > DATEADD('DAY',-2, CURRENT_TIMESTAMP())
You can try below using DATEADD() function
select * from table where date > DATEADD('DAY',-2, CURRENT_DATE)
I have a table with event and datetime. How can I simply select all events that occurred any day before 21:15 hr?
You can use the TIME() function to get just the time portion of a DateTime column and do the comparison that way:
SELECT *
FROM myTable
WHERE TIME(dateColumn) < '21:15:00';
A full list of useful MySQL date and time functions can be found at this link, which may be helpful for other comparisons in the future.
What is the function that would return, in minutes, the current time and another time in a field?
select mytable.mytimecolumn, now() from mytable
if you want the difference in minutes,
SELECT TIMESTAMPDIFF(MINUTE,now(),mytable.mytimecolumn) from mytable
Users of my site will be able to create events. They will set a date and a time separately. The event will be saved to a mysql database containing separate date and time columns.
I would like to query the database to return events which are up and coming but not passed. i.e. the event date is today or in the future and if the event date is today check the the time of the event hasn't passed.
I don't know the syntax for this and I cant seem to find it anywhere.
If anyone knows a good way to do this I'd very much appreciate your help.
The simplest way to express this efficiently in SQL and make use of any indexes you have on the date and/or time columns would be a query like this:
SELECT *
FROM your_table
WHERE date_column > current_date()
OR (date_column = current_date() AND time_column > current_time())
Depending on which version of MySQL you are using and how your table is indexed there is a small chance that the optimizer would prefer the query expressed as two SELECTs UNIONed together to avoid the OR:
SELECT *
FROM your_table
WHERE date_column > current_date()
UNION
SELECT *
FROM your_table
WHERE date_column = current_date()
AND time_column > current_time()
select * from table where concat_ws(' ',date_field,time_field) > now()
Why don't you use a datetime field?
in pseudocode
if ((date > today) OR (date == today AND time > now))
I'm working with a database that has date information stored as a Unix timestamp ( int(11) ) and what I want to do is only return entries from the past X days, the past 90 days for example.
What I've come up with is:
SELECT * FROM mytable WHERE category=1 AND
FROM_UNIXTIME( time ) > DATE_SUB(now(), INTERVAL 91 DAY)
Where 'time' is the int(11) in the db. This seems to be working fine, but just wondering what others think of this.
SELECT * FROM mytable WHERE category=1 AND
time > (UNIX_TIMESTAMP() - ((60*60*24)*90))
or simply
SELECT * FROM mytable WHERE category=1 AND
time > (UNIX_TIMESTAMP() - (86400*90))
this is just comparing a number (seconds in this case)
This query is bound to cause you headaches down the way as MySQL needs to do the conversion of dates for every row making use of indexes impossible. Unix timestamps are numbers, so instead of converting a timestamp to another date format, convert your lookup dates to unix timestamps.
What is the reason for storing the timestamp as an int ? I would use the mysql DATETIME data type because you can use the many Date Time functions mysql has.
If you do not have control over the data type of this field I would convert your date to the unix timestamp int before you do your query and compare it that way.
Just thinking aloud... wouldn't doing it the other way around cause less work for the DB?
time > UNIX_TIMESTAMP( DATE_SUB(NOW(), INTERVAL 91 DAY) )