I am trying to select all data from an SQlite database for the last 24 hours. There is a column 'Date' where the date is present and that is in EPOCH time. I've tried a variety of different commands but this seems to be what I have so far:
SELECT *
FROM Log
WHERE UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -1 DAY))
and
SELECT * FROM Log WHERE Date >= datetime('now', ' '-24 hours');
This doesn't seem to work. Ultimately, I am trying to convert the last 24 hours of this database to a CSV in Unix and have only the last 24 hours there using the following:
sqlite3 -header -csv /opt/demo/log_20170501131627.db " select * from Log
WHERE Date >= datetime('now', ' '-24 hours');" > /opt/demo/DB.csv
Any ideas?
SELECT * FROM Log WHERE Date >= strftime('%s', 'now', '-1 day');
You can try it here:
http://sqlfiddle.com/#!5/440d5/2/0
edit: simplified strftime usage based on "CL."-s comment below (thanks)
I'm not sure the precision of your epoch time. In case you are saving your epoch time to the millisecond, you can do it like this:
SELECT * FROM Log WHERE CAST((DATE/1000) AS LONG) >= strftime('%s', 'now', '-1 day');
The divide by 1000 above is just converting it to seconds and then casting it to a long (decimal values don't work if you are using '%s'). So if your epoch time is in seconds, just ignore the cast and division part of the above solution. Hope this helps!
Related
SELECT *
FROM SESSIONS
WHERE TIME_TO_SEC(TIMEDIFF(NOW(), SESSION_CREATED)) / 3600 >= 24
This give me 2 results
DELETE FROM SESSIONS
WHERE TIME_TO_SEC(TIMEDIFF(NOW(), SESSION_CREATED)) / 3600 >= 24
And this give me: "Error Code: 1292. Truncated incorrect time value"
SESSION_CREATED is TIMESTAMP Datatype
Actual data:
SESSION_ID SESSION_CREATED
223133 2017-05-22 07:14:34
223134 2017-05-22 07:14:36
How can the select work but not the delete?
Why are you using such a complicated expression? Why not just do:
DELETE FROM SESSIONS
WHERE SESSION_CREATED < NOW() - INTERVAL 1 DAY;
As for why your code might fail, it is using timediff() which is limited to the range of the time data type. And this is:
MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from
'-838:59:59' to '838:59:59'.
Because you are using NOW(), the values change from one iteration to the next. You just happened to run the SELECT when the data wasn't too old and then the DELETE when it was.
Example for Timediff using TIMESTAMPDIFF on MySQL:
To use TIMESTAMPDIFF, define the unit (SECOND, MINUTE, HOUR...), the initial and end date (must be timestamp's datatype).
ROUND( TIMESTAMPDIFF(HOUR, initial_date, CURRENT_TIMESTAMP()) / 24, 2 )
I have got a table with 2 columns epoch_start and epoch_end.
I want to find the difference in days of these 2 epochs.
The problem i am facing is that the above columns are character varying(5000) type.
The query im running is
select datediff(day,'1459762341','1450762341') as numdays;
The error i get is
ERROR: invalid input syntax for type timestamp: "1459762341"
I have found the solution -
To get timestamp from epoch -
SELECT (TIMESTAMP 'epoch' + '1459762341' * INTERVAL '1 Second ') as
mytimestamp
For datediff between two epochs -
select datediff(day,(TIMESTAMP 'epoch' + '1458762341' * INTERVAL '1 Second '), (TIMESTAMP 'epoch' + '1459762341' * INTERVAL '1 Second ')) as numdays;
"epoc" time is just the number of seconds from 1/1/1970, so its just some counter of the number of seconds from that date.
So, epic_start can really be thought of as start_seconds, and epic_end is really just end_seconds.
The fact that they are the count of the number of seconds from the same starting point is the only thing that really matters.
To get the number of days between two numbers representing seconds from the same starting point:
days = (end_seconds - start_seconds)/60/60/24
or
SELECT (end_seconds - epic_start)/60/60/24 AS numdays
Redshift will return an integer value without the decimal portion, so if the formula returns 1.9, numdays will be 1.
You are passing wrong parameters in datediff().
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate
Above will return the difference in days between two given dates.
Read more on datediff() here datediff
Our table has start_time and end_time as unix epoch timestamps. I'd like to find all the rows where the end_time is exactly 14 hours (50400) greater than the start time.
SELECT * FROM `mrbs_entry` WHERE (`end_time`-`start_time`)==50400
This seems slightly off, and in fact doesn't work. I'm not sure what the proper terminology to search though.
You shouldn't use == for comparing the values, but =.
Or you can use FROM_UNIXTIME to format is as date and then DATESUB with INTERVAL 14 HOUR. So it's:
SELECT * FROM `mrbs_entry`
WHERE DATE_SUB(FROM_UNIXTIME(`end_time`), INTERVAL 14 HOUR) = FROM_UNIXTIME(`start_time`);
I'm trying to get an mysql query similar to date_trunc in psql.
Unfortunate Mysql do not have date_trunc function and I found I can use extract instead in Mysql.
What I want to do is write a script which i will run let say 10 minutes past each hour but I want to only select data from begin of an hour till end of this hour.
For example I will run script 12:10 and I want to display data from 11:00:00 till 11:59:59.
In PSQL query would look like that:
SELECT *
FROM data
WHERE time > ( date_trunc('hour',now()) - interval '1 hour' )
AND time <= ( date_trunc('hour',now()) ) ORDER BY time;
I was trying to use extract in similar fashion but I have no rows returned or error :/
Query below returns for example some narrowed data but it's like 2 hours each day from day one when database was started not last hour only:
SELECT *
FROM data
WHERE extract(hour from cr_date) between extract(hour from now()) - interval 1 hour)
AND extract(hour from now())
ORDER BY cr_date;
Any ideas how this can be achieved? or what I'm doing wrong in this query?
Hour is only an integer, so it's finding any matches between , for example, 9 and 10, regardless of the date.
I would recommend
select * FROM data
where cr_date >= date(now()) + INTERVAL hour(now())-1 HOUR
and cr_date <= date(now()) + INTERVAL hour(now()) HOUR
date(now()) returns midnight, and hour(now()) returns the number of hours since midnight
so, at 11:10 am, it should result in a results between midnight + 10 hours (10 am) and midnight + 11 hours (11 am)
I want to know how much time has elapsed in seconds from a table field upload_date and now, when the query is executed. After reading some functions of MySQL I tried but wasn't so lucky.
Is there a way to return the value in hours if it was less than 24 hours, and in days, if it was 100 days or less? Otherwise I will have to code that in PHP.
The date is in CURRENT TIME STAMP format, just to clear that up.
I tried this:
SELECT DATE_SUB(NOW(), upload_date) FROM is_meeting_files
I also tried this, but it gave a difference of 0 seconds:
SELECT DATEDIFF(CURDATE(), upload_date) AS intval FROM is_meeting_files LIMIT 0,5;
You can subtract the unix timestamp values from one another:
SELECT unix_timestamp(NOW()) - unix_timestamp(upload_date) from is_meeting_files;
You can do it this way:
SELECT
IF (TIMESTAMPDIFF(HOUR, upload_date, NOW()) <= 24,
CONCAT(TIMESTAMPDIFF(HOUR, upload_date, NOW()), ' hours'),
IF (TIMESTAMPDIFF(DAY, upload_date, NOW()) <= 100,
CONCAT(TIMESTAMPDIFF(DAY, upload_date, NOW()), ' days'),
CONCAT(TIMESTAMPDIFF(MONTH, upload_date, NOW()), ' months'),
)
) as diff
FROM is_meeting_files;
This will return "n hours" when diff is less than 24 hours, "n days" if diff is less than 100 days and "n months" when diff is more than 100 days.
From my point of view it's a bad idea to do all this operations in SQL because such kind of queries is not possible to cache and as you see it is not so flexible. So, I recommend you to generate this on a PHP side.