如何在SQLite中按月和年选择

时间:2013-11-04 03:38:27

标签: sqlite winrt-xaml sqlite-net

在我的SQlite表中,它有DateTime字段示例:order_date

如何执行这些任务:

  1. 按月份检索记录,如2013年1月,2月,3月等。
  2. 使用上述条件(1)
  3. 按开始日期和结束日期检索记录

    由于

    How to convert `DateTime` for format requires by SQLite for (1) and (2) 
    
        DateTime  Dt = new DateTime.Today();
        Month-year ?  Year?
    
    
        Records = await db.QueryAsync("Select * From Purchase where order_date=" + "Month-Year");
    
    In SQLite , which operator is correct : `=` or `==`? 
    
    ----- Update:
    
    using SQlite:strftime
    
    SELECT * FROM tablename WHERE strftime('%Y', Order_Date) = '2013' AND strftime('%m', Order_Date) = '2';
    
    1) Is there a function to get month as number from DateTime?
    
    

1 个答案:

答案 0 :(得分:1)

我假设您使用的是

因此,您可以在查询中使用参数:

//Returning all records from February 2013
Records = await db.QueryAsync<Purchase>("Select * From Purchase where order_date >= ? and order_date < ?", new DateTime(2013, 2, 1), new DateTime(2013, 3, 1));