SQLite 3 でミリ秒付きの日時を扱う方法

前準備

SQLite 3の基本情報は別ページ »に詳しく解説されています.

ミリ秒を含む日時データのテーブル定義とレコード挿入

SQLite 3の起動方法

本例では,インメモリ・データベースを使用するため,データベース名を指定せずに起動します.

sqlite3

現在の日時の取得

select datetime('now', 'localtime');

日時データを格納するテーブルの作成と初期データの挿入

create table R ( id integer primary key not null, created_at datetime);
insert into R values( 1, '2020-06-25 19:30:30.001' );
insert into R values( 2, '2020-06-25 19:30:30.002' );
insert into R values( 3, '2020-06-25 19:30:30.003' );
insert into R values( 4, '2020-06-25 19:30:31.001' );

データ検索の具体例

select * from R;
select * from R where created_at < '2020-06-25 19:30:31.001';
select * from R where '2020-06-25 19:30:30.002' < created_at AND created_at < '2020-06-25 19:30:31.001';

UNIXタイムスタンプ形式での表示

select strftime("%s", created_at) + strftime("%f", created_at) - round( strftime("%f", created_at) ), created_at from R;

SQLite 3の終了手順

.exit