我的class / SQLite表中有一个DateTime类型的列/类成员。
但是,这个查询都是:
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken >= '11/11/2009' AND dateTimeTaken <= '11/11/2014'
- 这一个:
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN '11/11/2009' AND '11/11/2014'
...不返回任何记录。这两个似乎都是语法接受的(它们运行没有错误),但在LINQPad中返回“(0 items)”,即使这些日期之间有多个记录。
有人建议我使用这个SQL:
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN CONVERT(DATETIME,'2009-11-11') AND CONVERT(DATETIME,'2014-11-11');
...但是在测试中,LINQPad诙谐地告诉我,“SQLite错误没有这样的专栏:DATETIME”“
那么解决方案是什么?我需要什么查询语法/方法才能获得正确的数据子集?
这有效:
SELECT * FROM PhotraxBaseData WHERE dateTimeTaken&gt; =? AND dateTimeTaken&lt; =?
但是,要将DateTimeOffset中存储的日期转换为SQLite需要查看的日期,我编写了这个实用程序函数:
(我知道这非常冗长,但知道方式如何导致,我可能会留下另一天,但是......无论如何):
// Dates were in format "4/25/2008 10:38:56 AM - 07:00" which SQLite could not grok
// It needs it like:"2008-04-25 10:38:56
internal static String SQLitifiedDate(DateTimeOffset aDate, bool BeginningOfDay)
{
const string BEGIN_DAY = "00:00:00";
const string END_DAY = "23:59:59";
String timeStr = END_DAY;
if (BeginningOfDay)
{
timeStr = BEGIN_DAY;
}
DateTimeOffset dto = aDate;
int Year = dto.Year;
int Month = dto.Month;
int Day = dto.Day;
String year = Year.ToString();
String month = Month.ToString();
if (month.Length == 1)
{
month = "0" + month;
}
String day = Day.ToString();
if (day.Length == 1)
{
day = "0" + day;
}
return String.Format("{0}-{1}-{2} {3}", year, month, day, timeStr);
}