sqlite select where field time - sqlite

I have an sqlite table as follow:
I have to select the rows where 'start'< now time in order to have just the ongoing program; logically as:
if time now is 10:05 then the result should be:
How I can formulate the right select statement?
Thank u in advance for any help.

You can use the function strftime() to get the current time in the format HH:MM so that you can compare it to the column start:
SELECT *
FROM tablename
WHERE start < strftime('%H:%M', 'now');
If you want only the last row for each dpt:
SELECT *
FROM tablename
WHERE start < strftime('%H:%M', 'now')
GROUP BY dpt
HAVING MAX(start);

Related

How do I pull an extra row when pulling data from between dates?

I'd like to pull another row with the nearest date before the beginning of the following query from a sqlite db:
select * from inv WHERE TIME BETWEEN date1 AND date2
Is there a simple addition to the query or do I need to pull in more data and do the transformation at the end?
Thank you
Use UNION for the row with the maximum TIME that is less than date1:
select * from inv WHERE TIME BETWEEN date1 AND date2
UNION
select * from inv WHERE TIME = (select MAX(TIME) from inv WHERE TIME < date1)
If there would be more than 1 additional rows and you want exactly 1, you can add to the above code:
LIMIT 1

SQLite strftime Group By

I have a table consisting of a date field and a barcode field; I want the number of barcodes grouped by day for the previous month.
This looked like it would work:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date >= datetime('now', '-1 month')
GROUP BY day
ORDER BY date ASC;
But that gives me incorrect counts. E.g.:
341|30-01-2017
274|31-01-2017
288|01-02-2017
332|02-02-2017
224|03-02-2017
35|04-02-2017
1009|06-02-2017
1481|07-02-2017
1626|08-02-2017
507|09-02-2017
428|10-02-2017
125|11-02-2017
1838|13-02-2017
2591|
Whereas:
SELECT COUNT(*) FROM barcodes WHERE date LIKE '2017-02-10%';
579
If I do this:
SELECT
COUNT(*) AS count,
strftime('%d-%m-%Y',date) AS day
FROM barcodes
WHERE date LIKE '2017-02-10%'
GROUP BY day
ORDER BY date ASC;
I get:
428|10-02-2017
151|
So my question is: why is SQLite providing the result as two lines when I use strftime()?
%d-%m-%Y is not one of the supported date formats, so comparisons do not work correctly, and any of the built-in date functions will return NULL.

Subtracting and aggregating dates with millisecond precision

I have a table with two columns that contain date-times, in particular, a time_started and a time_ended. I want to determine the duration defined by these two times and sum all the durations.
This is my attempt:
select strftime('%H:%M:%S.%f', sum(duration), 'unixepoch')
from (
select strftime('%s', time_ended) - strftime('%s', time_started) as duration
from mytable
)
The reason why this doesn't work is that strftime('%s', time_started) returns the integer number of seconds since 1970-01-01. Which means I'm losing the milliseconds.
Is there any way to get the fractional number of seconds since 1970-01-01 instead?
Note that the date-time values have the same format of this example: "2013-04-24 14:57:30.661259".
Update, now using julianday(), by #LS_dev's suggestion in the answers:
select strftime('%H:%M:%f', sum(duration))
from (
select julianday(time_ended) - julianday(time_started) as duration
from mytable
)
Two issues remain though:
The results of the sum have more 12 hours than they should (!)
The milliseconds are being truncated to three digits, which looks like a limitation of strftime()
Use JULIANDAY:
select duration*24*60*60
from (
select JULIANDAY(time_ended) - JULIANDAY('%s', time_started) as duration
from mytable
)
EDIT: an "all-work-done-for-you" solution:
SELECT *, TIME("00:00", CAST(Duration AS INT)||' seconds')||SUBSTR(Duration-CAST(Duration AS INT), 2)
FROM (
SELECT *, (JULIANDAY(time_ended) - JULIANDAY(time_started))*60*60*24 as Duration
FROM mytable
)
Check in SQL Fiddle

Update only the year in SQLite column

I have a SQLite3 database that has 366 rows in it, and a date for each day of the year. I need to make sure that the year is current, and if it is not, then I need to update the year to the current calendar year. What I would like to do is something like the following:
UPDATE daily SET date = DATE('%Y', 'now');
or
UPDATE daily SET date = strftime('%Y', 'now');
But those queries just make the date column NULL, and even if they did work as I expected them to, I doubt that it would retain the day and month that is there already.
In MySQL, I can do something like UPDATE daily SET date = ADDDATE(date, INTERVAL 1 YEAR) -- but firstly, it is not a valid SQLite query, and secondly, I need to update to the current year, not just step up one year.
Any help would be appreciated.
Try this:
create table t (id int, d text);
insert into t
select 1, date('2011-01-01') union
select 2, date('2012-03-11') union
select 3, date('2013-05-21') union
select 4, date('2014-07-01') union
select 5, date('2015-11-11');
select * from t;
update t set
d = date(strftime('%Y', date('now')) || strftime('-%m-%d', d));
select * from t;
It uses Date And Time Functions. Firstly it takes month and day from field (strftime('-%m-%d', d)) then add (concatenate) current year (strftime('%Y', date('now'))) and converts it to date.
SQL Fiddle live example.

SQLite3 /Python: Update Query to populate a field by taking the difference from two fields in the same table

My table: tblTest:
RunID,StartTime, EndTime, Period
1,2013-03-30 18:08:14-04,2013-04-01 10:57:22-04
2,2013-04-03 12:13:10-04,2013-04-03 18:05:34-04
3,2013-04-04 06:02:30-04,2013-04-05 10:42:00-04
4,2013-04-05 10:43:00-04,2013-04-06 13:23:06-04
I am attempting to update the table to calculate the column Period.
The query that I am using is:
UPDATE tblTest SET Period = (SELECT strftime('%s',substr(endtime,1,19)) -
strftime('%s',substr(starttime,1,19)) From tblTest)
but to my surprise it updates all Periods with the same value from the first line.
What am I doing wrong?
A subquery like (SELECT ... FROM tblTest) without a WHERE condition returns all records of the table.
In a context where only one value is expected (like in the SET expression), only the first such record is used.
You can just directly access the columns of the table that you are updating:
UPDATE tblTest
SET Period = strftime('%s', substr(EndTime, 1, 19)) -
strftime('%s', substr(StartTime, 1, 19))

Resources