how to query in sqlite for different date format

前端 未结 4 1887
北荒
北荒 2020-12-15 06:51

I am using sqlite for local database in mobile and in my database, I have date field with column name RN_CREATE_DATE. My RN_CREATE_DATE

相关标签:
4条回答
  • 2020-12-15 07:17

    strftime works like this:

    sqlite> select strftime('%d-%m-%Y %H:%M:%S', datetime('now'));
    2012-09-13 12:42:56
    

    If you want to use it in a WHERE clause, replace datetime('now') with the datetime you want to match in the YYYY-mm-dd HH:MM:SS format.

    Example:

    SELECT *
      FROM t
     WHERE c = strftime('%d-%m-%Y %H:%M:%S', '2012-09-13 12:44:22');
    --                   dd-mm-YYYY HH:MM:SS  YYYY-mm-dd HH:MM:SS
    
    0 讨论(0)
  • 2020-12-15 07:28

    strftime always generates four-digit years, so you have to use substr to cut off the first two digits:

    ... WHERE RN_CREATE_DATE = strftime('dd-mm-', '2012-07-28 12:04:34') ||
                               substr(strftime('%Y %H:%M:%S', '2012-07-28 12:04:34'), 3)
    

    It would be easier to store the column values in a format that is directly understood by SQLite.

    0 讨论(0)
  • 2020-12-15 07:31

    I'd like to add some information about using SQLite Date and Time with UNIX epoch time:

    The UNIX epoch was 00:00:00 1970-01-01, i.e. midnight of 1st Jan 1970. The current time on UNIX machines (like those running Linux) is measured in seconds since that time.

    SQLite has support for this epoch time, and I've found it very useful for using it as the format for a DateTime field in SQLite.

    Concretely, suppose I want to have an Event table, for events like concerts etc. I want a fromDateTime field to store when the event starts. I can do that by setting the fromDateTime filed to type INTEGER, as such:

    CREATE TABLE IF NOT EXISTS Event(
        eventID                     INTEGER PRIMARY KEY,
        name                        TEXT,
        ratingOutOf10               REAL,
        numberOfRatings             INTEGER,
        category                    TEXT,   
        venue                       TEXT,
        fromDateTime                INTEGER,
        description                 TEXT,   
        pricesList                  TEXT,   
        termsAndConditions          TEXT                
    );
    

    Now, let's get to the usage of the UNIX epoch with SQLite DateTime fields:

    Basic
        select strftime('%s', '2016-01-01 00:10:11');               --returns 1451607012
        select datetime(1451607012, 'unixepoch');                   --returns 2016-01-01 00:10:11
        select datetime(1451607012, 'unixepoch', 'localtime');      --returns 2016-01-01 05:40:11 i.e. local time (in India, this is +5:30).
    
    Only dates and/or times:
        select strftime('%s', '2016-01-01');                        --returns 1451606400
        select strftime('%s', '2016-01-01 16:00');                  --returns 1451664000
        select date(-11168899200, 'unixepoch');                     --returns 1616-01-27        
        select time(-11168899200, 'unixepoch');                     --returns 08:00:00
    
    Other stuff:
        select strftime('%d-%m-%Y %H:%M:%S', '2012-09-13 12:44:22') --returns 13-09-2012 12:44:22
    

    Now, here's an example usage of the above with our Event table:

    EXAMPLE:
        insert into Event 
            (name, ratingOutOf10, numberOfRatings, category, venue, fromDateTime, description, pricesList, termsAndConditions)
            VALUES
            ('Disco', '3.4', '45', 'Adventure; Music;', 'Bombay Hall', strftime('%s','2016-01-27 20:30:50'), 'A dance party', 'Normal: Rs. 50', 'Items lost will not be the concern of the venue host.');
    
    
        insert into Event 
            (name, ratingOutOf10, numberOfRatings, category, venue, fromDateTime, description, pricesList, termsAndConditions)
            VALUES
            ('Trekking', '4.1', '100', 'Outdoors;', 'Sanjay Gandhi National Park', strftime('%s','2016-01-27 08:30'), 'A trek through the wilderness', 'Normal: Rs. 0', 'You must be 18 or more and sign a release.');
    
    
        select * from event where fromDateTime > strftime('%s','2016-01-27 20:30:49');
    

    I like this solution because it's really easy to work with programming languages, without too much thinking of the various formats involved in SQLite's DATE, TIME, DATETIME, etc. data types.

    0 讨论(0)
  • 2020-12-15 07:33

    strftime('%Y', '2012-07-28 12:04:34') returns 2012, as:

    sqlite> select strftime('%Y', '2012-07-28 12:04:34');
    2012
    

    but the RN_CREATE_DATE is of type datetime, which expect a full datetime like '2012-07-28 12:04:34'. what you want might be simply:

    SELECT  RN_CREATE_DATE 
    FROM DISPOSITION 
    WHERE  RN_CREATE_DATE='2012-07-28 12:04:34'
    
    0 讨论(0)
提交回复
热议问题