2017-05-19 19 views
1

私はこれらの2つの日付の違いを見つけようとしており、数日、時間、分の数で表示しています。ここで日付間のSQLiteの相違

5/18/17午前4時57分PM

select *,strftime('%H:%M',CAST ((julianday('Completed Date') - julianday('Created Date')) AS REAL),'12:00') from report 
+0

これは[有効な日付形式](http://stackoverflow.com/documentation/sqlite/5252/data-types/18688/date-time-types)ではありません。 –

+0

私は1つの日付しか見ることができません。もう少し与えることができますか?フォーマットを説明できますか(「月/日/年h:mm」と思われますが)?どのように出力をしたいですか? – Yunnosch

答えて

0

...彼らは今フォーマット方法であり、以下に私が作ってみたが、動作するように見えるdoesntのものです5/18/17 4:57 PMはSQLiteで有効な時刻文字列形式ではないので、このような形式のstrftimeを使用することは意味がありません。 YYYY-MM-DDまたはYYYY-MM-DD HH:MM:SSのような有効な時間文字列形式の場合にのみstrftimeを使用できます。

Created DateCompleted Dateの差を計算する場合は、その2つの日付の間の秒数を計算し、それを使用して日数、時間、分を計算する必要があります。

SELECT strftime('%s',"Completed Date") - strftime('%s', "Created Date") from report; 

1日に86400秒、1時間に3600秒、1分に60秒があります。あなたはここから残りを計算できるはずです。

+0

@ CLがコメントに表示されているように間違った日付形式が回答に影響しますか?その光の中で、あなたの答えはその点を見逃しているようです。 – Yunnosch

+0

私の答えは、2つのdatetimeカラムを持つ 'report'テーブルに基づいています:' Created Date'と 'Completed Date'。間違った日付形式が何であるか分かりません。 OPは 'strftime( '%H:%M')'を使用して、2つの日付の間の時間と分を取得したいと思われます:CreatedとCompleted。しかし、これはうまくいかないでしょう。 2つの日付の違いを見つける唯一の方法は、 'strftime( '%s')'を使用して秒単位で差分を求め、その値を日、時間、分で計算することです。 – kimbaudi

+0

OPによって与えられた日付の例である '5/18/17 4:57 PM'でstrftimeが動作するかどうか試しましたか?それはSQLite SQLite 3.18.0ではありません。 – Yunnosch

0

データベース構造の日付形式を変更できない、または変更しないと仮定します。それが可能なのであれば、そうすることができます。
出力(下を見てください)は異なる日付フォーマットを持っていますが、表示するだけで、あなた自身のフォーマットを出力に保つことはもちろん可能です。

私が最初

  • は、個々のフォーマット
  • はstrftimeの
  • に適した二つの日付にそれらを配置し直す再利用可能な部分に日付を分割

    • に、いくつかの共通テーブル式を使用していますその中には、重い弦の操作と少し大胆な "+12"/"+ AM"があります。

      次に、kimbaudis提案を計算に使用します。
      すぐにすぐに行くためのキンバディへのクレジット
      私はジュリアンの日で遊んでみる時間を失った。

      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