Tôi lưu trữ ngày là 'DD-MON-YYYY format (10-Jun-2016) và truy vấn bên dưới hoạt động để tôi tìm kiếm hồ sơ giữa 2 ngày.
select date, substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), case
substr(date, 4,3)
when 'Jan' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jan' , '01'))
when 'Feb' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Feb' , '02'))
when 'Mar' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Mar' , '03'))
when 'Apr' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Apr' , '04'))
when 'May' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'May' , '05'))
when 'Jun' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jun' , '06'))
when 'Jul' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Jul' , '07'))
when 'Aug' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Aug' , '08'))
when 'Sep' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Sep' , '09'))
when 'Oct' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Oct' , '10'))
when 'Nov' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Nov' , '11'))
when 'Dec' then strftime('%s', replace(substr(date,8,11) || '-' || substr(date,4,4) || substr(date, 1,2), 'Dec' , '12'))
else '0' end as srcDate from payment where srcDate >= strftime('%s', '2016-07-06') and srcDate <= strftime('%s', '2016-09-06');
+1: Nếu ngày đã được định dạng theo thứ gì đó mà chức năng xử lý ngày của SQLite hiểu, có thể làm điều gì đó thông minh hơn. (Hoặc nếu ngày đã được chuyển đổi sang ngày julian trước khi lưu trữ trong DB.) Nhưng nó đã không được, và "ngày" cần phải được sửa chữa. Tất nhiên, anh ta cũng có thể thử thêm chức năng 'to_date' của riêng mình, nhưng làm thế nào để làm điều đó phụ thuộc vào những gì SQLite được nhúng vào (mà chúng ta không biết). –
@Donal tái tạo chức năng 'to_date' của riêng mình, cá cược mà không phải tất cả các chuỗi trong trường đó không phải là ngày hợp lệ :-) –