0

I've got a column / class member of type DateTime in my class/SQLite table.

However, both this query:

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken >= '11/11/2009' AND dateTimeTaken <= '11/11/2014'

-and this one:

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN '11/11/2009' AND '11/11/2014'

...return no records. Both of these seem to be accepted syntax-wise (they run without error), but return "(0 items)" in LINQPad, even though there are multiple records between those dates.

It was suggested to me to use this SQL:

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken BETWEEN CONVERT(DATETIME,'2009-11-11') AND CONVERT(DATETIME,'2014-11-11');

...but in testing that, LINQPad testily told me, "SQLite error no such column: DATETIME""

So what is the solution? What query syntax/methodology do I need to get the correct subset of data?

UPDATE

This works:

SELECT * FROM PhotraxBaseData WHERE dateTimeTaken >= ? AND dateTimeTaken <= ?

UPDATE 2

However, to convert the date as stored in DateTimeOffset to what SQLite needs to see, I wrote this utility function:

(I know this is very verbose, but knowing how way leads on to way, I'll probably leave it for another day, but...anyway):

// 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);
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

0 Answers0