I have a table which stores time data in the DATETIME format. I would like to build a query to return just the values after a give time (for example 15:00 - i.e. 3pm) for each day, not just for a given date.
Is this possible in MySQL. Does anyone have any examples?
You can use the hour() function:
where hour(col) >= 15
You can do this:
WHERE TIME(column) >= '15:00:00'
or even
WHERE TIME(a.column) >= TIME(b.cutoff)
Related
The problem is that I am currently trying to resolve is to select a date time value (iso 8601) and compare it with the sysdate and time, and the sysdate/time has to be 20 min in the past.
I have tried using TO_TIMESTAMP and tried to convert the SYSDATE but the problem keeps returning.
select * from table
where timestamp_from_the_table > to_date(systimestamp, 'YYYY-MM-DD"T"hh24:mi:ss.ff3TZR') -INTERVAL '30' MINUTE
The result I would like is to have a list with the timestamps from table but filtered with the time only from 20 min in the past not longer.
Hope someone can help me. Thanks in advance.
Never store date values as string, i.e. VARCHAR2!
In your case you can run
WHERE TO_TIMESTAMP_TZ(timestamp_from_the_table, 'YYYY-MM-DD"T"hh24:mi:ss.ff3TZR')
> systimestamp - INTERVAL '30' MINUTE
It might be an option to create a virtual column, i.e.
ALTER TABLE your_table ADD (real_timestamp TIMESTAMP(3) WITH TIME ZONE (TO_TIMESTAMP_TZ(timestamp_from_the_table, 'YYYY-MM-DD"T"hh24:mi:ss.ff3TZR') ));
Then you could simply run
WHERE real_timestamp > systimestamp - INTERVAL '30' MINUTE
I have a DateTime column in a MySQL table that I'm trying to filter just from the time of the DateTime. So for example I need all rows that the time is 8am-9am on any date. Is this possible? I've looked through all the docs and can't find anything of the sort.
Thanks guys!
Use the TIME function, like this
where TIME(myColumn) >= '08:00:00' and TIME(myColumn) <= '09:00:00'
TIME gets the time part of your value.
I am running queries for a ticketing system. I want to extract all tickets created after 6:00PM or 18:00:00 in database/military time.
Could I use a DATEPART function or EXTRACT function?
Something like this may work:
SELECT *
FROM ticket
WHERE TIME IS AFTER 6PM
The issue is that the datetime format is as '2014-12-01 16:13:38' so I would need to specify for only the characters after the DATE section.
In MySQL, just use the hour() function:
SELECT *
FROM ticket
WHERE hour(time) >= 18;
first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?
Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.
I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime