I got two different datetimes: 2020-05-18 12:30:01 and 2020-05-17 13:00:00.
I want to show the time difference between them in the format HH:MM:SS, which is 23:30:01.
If the difference is higher than 24 hours, let's say 28 hours, 12 minutes and 45 seconds, it would show like 28:12:45.
How can I do that in SQLite?
SQLite supports a limited number of functions for datetime manipulation.
One of these functions is strftime(), and
strftime('%s', somedate)
returns the number of seconds from '1970-01-01' up to somedate.
With the use of this function, arithmetic calculations, string padding and concatenations you can get what you want like this:
CASE WHEN ((strftime('%s', date1) - strftime('%s', date2)) / 3600) < 10 THEN '0' ELSE '' END ||
((strftime('%s', date1) - strftime('%s', date2)) / 3600) || ':' ||
SUBSTR('0' || (((strftime('%s', date1) - strftime('%s', date2)) / 60) % 60), -2) || ':' ||
SUBSTR('0' || ((strftime('%s', date1) - strftime('%s', date2)) % 60), -2)
Replace date1 and date2 with your dates.
See a simplified demo.
SELECT time(
(
julianday('2020-05-18 12:30:01')-
julianday('2020-05-17 13:00:00')
)*60*60*24, 'unixepoch'
);
answsers the question when time difference is lower than 24h...
I have a database that represent music shop data.
Tables I want to work with are (artist, album, track)
every album has an albumID, title and artistID
and every track has TrackID, Milliseconds, albumID and name
well, as you know every album has many tracks
I want to calculate the sum of all songs in the album(in Milliseconds) Then get the average of that sum to get the albums that are above the average in length.
I managed to calculate the length of every album but i struggled to get the average.
I want to generate a table that will be like that.
---------------------------------------------------------------------
AlbumID | Tile | Milliseconds
---------------------------------------------------------------------
1- | |
2- | |
3- | |
...
10- | |
----------------------------------------------------------------------
Sample table of Track
TrackID | Name | AlbumId | Milliseconds
1 For Those About To Rock 1 343719
6 Put The Finger On You 1 205662
7 Let's Get It Up 1 233926
85 Cochise 10 222380
86 Show Me How to Live 10 277890
87 Gasoline 10 279457
88 What You Are 10 249391
89 Like a Stone 10 294034
99 Your Time Has Come 11 255529
100 Out Of Exile 11 291291
101 Be Yourself 11 279484
102 Doesn't Remind Me 11 255869
111 Money 12 147591
112 Long Tall Sally 12 106396
so, LIMIT 10
and the length of the track
I believe that the following may be what you wish :-
WITH albumsums(id,asum) AS (
SELECT albumid, sum(milliseconds)
FROM track
GROUP BY albumid
)
SELECT album.albumid, album.title, asum
FROM album
JOIN albumsums
ON album.albumid = albumsums.id
WHERE asum > (SELECT avg(asum) FROM albumsums)
LIMIT 10;
Consider the following demo :-
DROP TABLE IF EXISTS track;
DROP TABLE IF EXISTS artist;
DROP TABLE IF EXISTS album;
CREATE TABLE IF NOT EXISTS track (trackid INTEGER PRIMARY KEY, name TEXT, milliseconds INTEGER, albumid);
CREATE TABLE IF NOT EXISTS artist (artistid INTEGER PRIMARY KEY, artistname TEXT);
CREATE TABLE IF NOT EXISTS album (albumid INTEGER PRIMARY KEY, title TEXT, artistid INTEGER);
INSERT INTO artist (artistname) VALUES ('Pink Floyd'),('Genesis'),('Deep Purple');
INSERT INTO album (title,artistid) VALUES('Dark side of the moon',1),('Fireball',3),('Foxtrot',2);
INSERT INTO track (name,milliseconds,albumid) VALUES
('Supper''s Ready',((22 * 60) + 57) * 1000,3),
('Watcher of the Skies',((7 * 60) + 21) + 1000,3),
('Time Table',((4 * 60) + 47) * 1000,3),
('Get ''Em Out by Friday',((8 * 60) + 35) * 1000,3),
('Can-Utility and the Coastliners',((5 * 60) + 45 ) * 1000,3),
('Speak to me /Breath',((3 * 60) + 58) * 1000,1),
('On the Run',((3 * 60) + 35) * 1000,1),
('Time',((7 * 60) + 5) * 1000,1),
('The Great Gig in the Sky',((4 * 60) + 44) * 1000,1),
('Money',((6 * 60) + 23) * 1000,1),
('Use and Them',((7 * 60) + 50) * 1000,1),
('Any Colour you Like',((3 * 60) + 26) * 1000,1),
('Brain Damage',((3 * 60) + 47) * 1000,1),
('Fireball',((3 * 60) + 24) * 1000,2),
('No No No',((6 * 60) + 54) * 1000,2),
('Demon''s Eye',((5 * 60) + 21) * 1000,2),
('Anyone''s Daughter',((4 * 60) + 43) * 1000,2),
('The Mule',((5 * 60) + 21) * 1000,2),
('Fools',((8 * 60) + 19) * 1000,2),
('No One Came',((6 * 60) + 34) * 1000,1),
('Strange Kind of Woman',((4 * 60) + 07) * 1000,1)
;
SELECT * FROM artist;
SELECT * FROM album;
SELECT * FROM track;
SELECT albumid, sum(milliseconds)
FROM track
GROUP BY albumid
;
WITH albumsums(id,asum) AS (
SELECT albumid, sum(milliseconds)
FROM track
GROUP BY albumid
)
SELECT album.albumid, album.title, asum, (SELECT avg(asum) FROM albumsums AS album_average_for_demo)
FROM album
JOIN albumsums
ON album.albumid = albumsums.id
WHERE asum > (SELECT avg(asum) FROM albumsums);
The net result (as you wanted above average) being just one album above the average :-
The CTE (Common Table Expression albumsums) as demonstrated by the previous query produces 3 rows (1 per album) with the album id and the sum of the tracks :-
Thus the average album length is 2552147 and thus only 1 album is greater than that (as would be expected from such a limited amount of data).
The tables (after being loaded) being :-
I have a datetime column in lotus notes which has value like 01/02/2019 01:01:01 PM.
I need to export the data into csv file in the below format:
2019-01-02 13:01:01.000
Tried changing the column properties in the view but while exporting the data is changed to 01/02/2019 01:01:01.
Here AM/PM is getting ignored.
Please suggest a way to do this properly.
Thanks in advance.
Use this formula in your view column, where DateField is the field with date.
year := #Text(#Year(DateField));
month := #Right("0" + #Text(#Month(DateField)) ; 2);
day := #Right("0" + #Text(#Day(DateField)) ; 2);
hour := #Right("0" + #Text(#Hour(DateField)) ; 2);
minute := #Right("0" + #Text(#Minute(DateField)) ; 2);
second := #Right("0" + #Text(#Second(DateField)) ; 2);
year + "-" + month + "-" + day + " " +
hour + ":" + minute + ":" + second + ".000"
Those #Right functions will pad your values with zeroes when needed.
I am trying to subtract two TIMESTAMP filed and want to get the difference in minute.
SELECT EXTRACT(MINUTE FROM ( CAST(END_DT AS TIMESTAMP(0)) - CAST(START_DT AS TIMESTAMP(0)) MINUTE(4) TO SECOND)) + EXTRACT(SECOND FROM (CAST(END_DT AS TIMESTAMP(0)) - CAST(START_DT AS TIMESTAMP(0)) MINUTE(4) TO SECOND)) * 1.00 / 60
from tableA
But I am getting interval filed overflow error.
I am able to achieve the solution using below
(EXTRACT(HOUR FROM TIME_DIFF) * 60) + EXTRACT(MINUTE FROM TIME_DIFF) + (EXTRACT(SECOND FROM TIME_DIFF) * 1.00 / 60)
where TIME_DIFF is (ACTL_END_DT - ACTL_START_DT) DAY(4) TO SECOND
I am trying to express the difference of two given dates in days, hours, and minutes (like 1 day, 6 hours, 17 minutes.) as SQLite query output. I have entryin and entryout as datetime fields in a SQLitedatabase. I tried all combinations of julianday and strftime but still running into rough weather.
I tried strftime('%d %H:%M', julianday(entryout)-julianday(entryin)). For a row the values are 2011-11-10 11:46, and 2011-11-09 09:00. but the output is 25 14:46 instead of 01 02:46.
Can some one help me with this, or point me correct logic for this? Thanks in advance.
You can try something like this:
SELECT
CAST((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) / (60 * 60 * 24) AS TEXT) || ' ' ||
CAST(((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) / (60 * 60) AS TEXT) || ':' ||
CAST((((strftime('%s', '2011-11-10 11:46') - strftime('%s', '2011-11-09 09:00')) % (60 * 60 * 24)) % (60 * 60)) / 60 AS TEXT);