How to correctly use times in SQLITE? - sqlite

I have a table which runs a DELETE query every few seconds. With that query I want to delete all entries that are older than 30 seconds.
At the moment I have a value for the creation time in my table creationdate integer which contains the date of creation in milliseconds since 1 January 1970.
In my DELETE query that I run every 30 seconds I compare it against the current time:
delete from table where creationdate < strftime('%s', 'now', '-30.0 seconds')
I referred to this article for the strftime formatting.
This query doesn't delete the entries, that's my problem. It works without the where statement so I guess that the function is the problem.
What am I missing out or is there even a better way to solve this problem?
edit: It doesn't work without the -30.0 seconds modifier either, so I think that I misunderstood how strftime works. Referred to this question as well.

I found my problem. I overlooked the fact that in SQLite strftime('%s') returns in SECONDS and not in MILLISECONDS.

Related

Ms-Access convert datetime to seconds since epoch [duplicate]

I'm trying to write a query for an MS Access 2007 connection to a MySQL database through ODBC. Everything's working fine, and the query does what I want it to do. The part that I'm hung up on is that I'm stuck asking the user for unix epoch time, instead of a regular date.
I looked up a bunch of references for MS Access, and while there are a number of date conversion functions I can use in the SQL call, I can't find any that I can use to convert from a normal date -> unix epoch date.
What I would like, and I assume this works, is to ask the user for the date in a much kinder fashion (a human readable date), and then convert it into unix epoch date. Now that I think about it, I guess my other option is to convert the unix epoch dates in the database after drawing them out with the SQL query, but I'd rather convert the user's input if at all possible as there is less of that input so I wouldn't have to do as much work.
SELECT TOP 5 Count( * ) AS [Number of visits by language], login.lang AS [Language]
FROM login, reservations, reservation_users
WHERE (reservations.start_date Between [Starting unix epoch time] And [Ending unix epoch time]) And reservations.is_blackout=0 And reservation_users.memberid=login.memberid And reservation_users.resid=reservations.resid And reservation_users.invited=0
GROUP BY login.lang
ORDER BY Count( * ) DESC;
Don't know if this is going to work (don't have Access to confirm), but try the suggestion from here:
http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx
which is to use:
DATEDIFF(second, '1 Jan 1970', tbl.LastChangeDate)
This is from some of the comments.
EDIT: See the comment from Remou.
Using MS 2010 I found this works: DateAdd("s",([epoch timestamp]-21600),#1/1/1970#)
http://www.pcreview.co.uk/forums/convert-epoch-date-t2324318.html

SQLite: How to convert a bigint field to date or timestamp?

I've received a SQLite file with .db extension.
Opening it, I have a column with serial numbers: i.e. 1600414704594 (that should correspond to 2020-10-09 and whatsover time)
The db comes from outside and I don't know how that date field has been built.
Could you suggest me a query to get a valid date/time from that db column ?
I've tried reading this post but none of the given solution returned me a valid (and actual) date, please help me.
It looks like an Unix time in milliseconds. SQLite's unixepoch modifier expects it in seconds. The conversion is fairly easy :
SELECT DATETIME(1600414704594 / 1000, 'unixepoch')
2020-09-18 07:38:24

SQLite - Ordering

I have a strange issue where upon selecting information from a SQLite database, ans ordering based upon date, the results returned are invalid.
My SQL statement is as such:
Select pk from usersDates order by datetime(usersDate, 'localtime') ASC
I have dates stored in the database which range as far as 2111. However the order the data is returned in indicates that dates from 2036 happen after the ones from 2111.
The column 'usersDate is actually a double (time interval since 1970 / unix time) - hence the reason for the cast.
Does anyone know what would cause this?
You should re-read the date and time syntax. The 'localtime' modifier expects an UTC time on its left.
Use SELECT pk FROM usersDates ORDER BY datetime(usersDate, 'unixepoch', 'localtime') ASC.

Updating records older than n minutes

I have a sqlite database with a datatime field called last_accessed and another field called stale.
How do I update the stale field for records older than say 5 minutes?
e.g.
This doesn't seem to work.
update mytable set stale=1 where (datetime('now') - last_accessed) > 300;
In fact
select datetime('now') - '2011-02-22 08:48:34';
returns 0.
How do I do arithmetic in sqlite3?
EDIT:
Since I'm converting last_accessed to a datetime and really it just needs to be a seconds field. Do you think I should convert that field to seconds field instead. Then I won't need to do something like strftime('%s',time('now')) - strftime('%s',last_accessed)?
Edit2:
There is no difftime function as far as I can tell. I discovered you have to use strftime('%s', datetime('now')) - ... format to make it work. However, instead I've converted simply to an integer field where I'm storing time(NULL) directly.
I think it should be
select datetime('now') - datetime('2011-02-22 08:48:34');
or something like that (see the examples).
To answer this question in case others are wondering how to correctly do date/time arithmetic in sqlite.
The correct answer is:
update mytable set stale = 1 where strftime('%s', datetime('now')) - strftime('%s', last_accessed) > 300;
However, I don't actually need the datetime format in the table and have instead opted for a numeric field instead. I'm not seeking any more answers to this question.
Thanks.

Time diff calculations where date and time are in seperate columns

I've got a query where I'm trying to get the hours in duration (eg 6.5 hours) between two different times.
In my database, time and date are held in different fields so I can efficiently query on just a startDate, or endDate as I never query specifically on time.
My query looks like this
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(endTime,startTime)),0) FROM events WHERE user=18
Sometimes an event will go overnight, so the difference between times needs to take into account the differences between the dates as well.
I've been trying
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(CONCAT(endDate,' ',endTime),CONCAT(startDate,' ',startTime))),0) FROM events WHERE user=18
Unfortunately I only get errors when I do this, and I can't seem to combine the two fields into a single timestamp.
Pretty sure your problem is that your concatenated values are being sent to TIMEDIFF() as strings rather than DATETIMEs. Try calling the DATETIME function on them:
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(DATETIME(CONCAT(endDate,' ',endTime)),DATETIME(CONCAT(startDate,' ',startTime)))),0) FROM events WHERE user=18
I don't have a MySQL DB in front of my to test that, but I think that or some similar form of it is what you are looking for. There's an example of it in the MySQL docs involving MICROSECOND:
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Edit: Hmm... looks like TIMEDIFF is supposed to work with strings. Worth trying anyway.
TIMEDIFF(endDate,startDate) + TIMEDIFF(endTime,startTime)

Resources