How to compare two dates in SQLite?

前端 未结 3 535
孤独总比滥情好
孤独总比滥情好 2021-02-07 18:39

I kind of assumed it was a string, so I compared it as a string, but not surprisingly it failed. I believe thats how it works in Mysql. I could be wrong as I haven\'t worked on

相关标签:
3条回答
  • 2021-02-07 19:20

    From Datatypes In SQLite Version 3:

    1.2 Date and Time Datatype

    SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

    • TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
    • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
    • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

    Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

    If you look at the examples in Date And Time Functions, something like this should get you close to what you want (which, I'm assuming, is 6:00 of the current day):

    WHERE b.start_time = date('now', 'start of day', '+6 hours')
    
    0 讨论(0)
  • 2021-02-07 19:32

    Sqlite can not compare dates directly. we need to convert them in seconds as well as integer also.

    Example

    SELECT * FROM Table  
    WHERE  
    CAST(strftime('%s', date_field)  AS  integer) <=CAST(strftime('%s', '2015-01-01')  AS  integer) ;
    
    0 讨论(0)
  • 2021-02-07 19:34

    SQLite doesn't have a dedicated DATETIME type. Normally what people do is make sure they store the date as a formatted string that is consistent; for example, YYYY-MM-DD hh:mm:ss. If you do so, as long as you're consistent, then you can compare dates directly:

    SELECT * FROM a WHERE q_date < '2013-01-01 00:00:00';
    

    This works because even though the comparison is technically an alphabetical comparison and not a numeric one, dates in a consistent format like this sort alphabetically as well as numerically.

    For such a schema, I would suggest storing dates in 24-hour format (the above example is midnight). Pad months, days, and hours with zeros. If your dates will span multiple timezones, store them all in UTC and do whatever conversion you need client-side to convert them to the local time zone.

    Normally dates and times are stored all in one column. If you have to have them separated for whatever reason, just make sure you dates are all consistent and your times are all consistent. For example, dates should all be YYYY-MM-DD and times should all be hh:mm:ss.

    The reason that YYYY-MM-DD hh:mm:ss is the preferred format is because when you go from the largest date interval (years) to the smallest (seconds), you can index and sort them very easily and with high performance.

    SELECT * FROM a WHERE q_date = '2012-06-04 05:06:00';
    

    would use the index to hone in on the date/time instead of having to do a full table scan. Or if they're in two separate rows:

    SELECT * FROM a WHERE q_date = '2012-06-04' AND q_time = '05:06:00';
    

    The key is to make sure that the dates and times are in a consistent format going into the database. For user-friendly presentation, do all conversion client-side, not in the database. (For example, convert '2012-06-04 05:06:00' to "1:06am Eastern 6/4/2012".)

    If this doesn't answer question, could you please post the exact format that you're using to store your dates and times, and two example dates that you're trying to compare that aren't working the way you expect them to?

    0 讨论(0)
提交回复
热议问题