how to query in sqlite for different date format

前端 未结 4 1888
北荒
北荒 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: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.

提交回复
热议问题