Angenommen, Sie können oder wollen nicht die Datumsformate Ihrer Datenbankstruktur ändern. Wenn das möglich ist, dann mach es einfach und viel einfacher.
Die Ausgabe (siehe unten) hat ein anderes Datumsformat, nur zum Anzeigen, das eigene Format in der Ausgabe zu behalten ist natürlich möglich.
Ich verwende einige allgemeinen Tabellenausdrücke, zu
- ersten Teil der Daten in wiederverwendbaren Teil
- fixieren die einzelne Formate
- ordnen sie in zwei Termine für strftime
Es gibt einige schwere Saitenmanipulationen und ein wenig gewagte "+12"/"+ AM" in der Mitte.
Dann benutze ich Kimbaudis Vorschlag für die Berechnung.
Dank an Kimbaudi, dass ich gleich zu Sekunden gegangen bin,
Ich habe einige Zeit verloren, als ich versuchte, mit julianischen Tagen zu spielen.
WITH parts -- splitting for reuse
( wrongcomonth,
codayyear, wrongcohours, wrongcomins, wrongcom,
wrongcrmonth,
crdayyear, wrongcrhours, wrongcrmins, wrongcrm
) as
(select
substr(Completed_Date, instr(Completed_Date, '/'), -2) as wrongcomonth,
substr(Completed_Date, instr(Completed_Date, ' '), -5) as codayyear,
substr(Completed_Date, instr(Completed_Date, ':'),-2) as wrongcohours,
substr(Completed_Date, instr(Completed_Date, ':')+1,2) as wrongcomins,
substr(Completed_Date, instr(Completed_Date, 'M')+1,-2) as wrongcom,
substr(Created_Date, instr(Created_Date, '/'), -2) as wrongcrmonth,
substr(Created_Date, instr(Created_Date, ' '), -5) as crdayyear,
substr(Created_Date, instr(Created_Date, ':'),-2) as wrongcrhours,
substr(Created_Date, instr(Created_Date, ':')+1,2) as wrongcrmins,
substr(Created_Date, instr(Created_Date, 'M')+1,-2) as wrongcrm
from report),
-- another "with"
pretty -- fixing formats
( coyear, comonth, coday, cohours, comins,
cryear, crmonth, crday, crhours, crmins
) as
(select
'20'||substr(codayyear, -2) as coyear,
substr('0'||wrongcomonth, -2) as comonth,
substr(replace(replace('_'||codayyear, '_/','_0'), '_', ''), 1,2) as coday,
substr('0'||(wrongcohours+replace(wrongcom, 'PM', '12')), -2) as cohours,
substr('0'||replace(wrongcomins, ' ', ''), -2, 2) as comins,
'20'||substr(crdayyear, -2) as cryear,
substr('0'||wrongcrmonth, -2) as crmonth,
substr(replace(replace('_'||crdayyear, '_/','_0'), '_', ''), 1,2) as crday,
substr('0'||(wrongcrhours+replace(wrongcrm, 'PM', '12')), -2) as crhours,
substr('0'||replace(wrongcrmins, ' ', ''), -2, 2) as crmins
from parts),
-- another "with"
times -- two, in correct format and order
(Completion_Time, Creation_Time
) as
(select
coyear||'-'||comonth||'-'||coday||' '||cohours||':'||comins as Completion_Time,
cryear||'-'||crmonth||'-'||crday||' '||crhours||':'||crmins as Creation_Time
from pretty)
-- now the real thing, as proposed by kimbaudi
select Creation_Time, Completion_Time,
((strftime('%s', Completion_Time)-strftime('%s', Creation_Time))/(24*60*60))||'d '||
(((strftime('%s', Completion_Time)-strftime('%s', Creation_Time))/(60*60))%24)||'h '||
(((strftime('%s', Completion_Time)-strftime('%s', Creation_Time))/(60))%60)||'m'
from times;
MCVE .dump
, mit einigen unterschiedlichen Terminen und eine zweite Linie für die Prüfung:
-- cleanup
drop table if exists report;
-- make playground
BEGIN TRANSACTION;
CREATE TABLE report ("Completed_Date" date, "Created_Date" date);
INSERT INTO report VALUES('5/8/17 10:57 AM','3/8/17 9:33 AM');
INSERT INTO report VALUES('12/31/16 4:1 PM','10/10/16 7:31 PM');
COMMIT;
Ausgang (beachten Sie die absichtlich unterschiedlichen Terminen für die Prüfung, im Gegensatz zu dem Zeitpunkt OP gab):
2017-03-08 09:33|2017-05-08 10:57|61d 1h 24m
2016-10-10 19:31|2016-12-31 16:01|81d 20h 30m
Dies ist kein [gültiges Datumsformat] (http://stackoverflow.com/documentation/sqlite/5252/data-types/18688/date-time-types). –
Ich sehe nur ein Datum. Kannst du noch etwas geben? Kannst du das Format erklären (obwohl es scheint "Monat/Tag/Jahr h: mm")? Wie möchten Sie die Ausgabe? – Yunnosch