我正在尝试找出这两个日期之间的差异,并将其显示为天数,小时数,分钟数。
以下是它们现在的格式,以下是我提出的但它似乎无法正常工作......
5/18/17 4:57 PM
select *,strftime('%H:%M',CAST ((julianday('Completed Date') - julianday('Created Date')) AS REAL),'12:00') from report
答案 0 :(得分:1)
5/18/17 4:57 PM
在SQLite中不是有效的时间字符串格式,因此将strftime
用于这种格式是没有意义的。您只能将strftime
与YYYY-MM-DD
或YYYY-MM-DD HH:MM:SS
等有效时间字符串格式一起使用。
如果您要计算Created Date
和Completed Date
之间的差异,则需要计算这两个日期之间的秒数,并使用它来计算天数,小时和分钟。
SELECT strftime('%s',"Completed Date") - strftime('%s', "Created Date") from report;
一天有86400秒,一小时有3600秒,一分钟有60秒。你应该可以从这里计算其余部分。
答案 1 :(得分:0)
假设您不能或不想更改数据库结构的日期格式。如果可能这样做,那就更容易了 输出(见下文)有不同的日期格式,仅用于显示,当然可以在输出中保留您自己的格式。
我正在使用一些常用的表表达式
有一些沉重的字符串操作和一点点大胆" + 12" /" + AM"在它中间。
然后我使用kimbaudis提议进行计算 归功于kimbaudi直接秒,。 我失去了一些时间试图玩朱利安日。
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
,有一些不同的日期和第二行用于测试:
-- 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;
输出(注意测试的故意不同日期,与OP给出的日期相反):
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