how to query in sqlite for different date format

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 column value is in this format 'dd-mm-yy HH:MM:SS' and I want to fetch data with given date. What will be the exact query for that I tried with below database column and value


2012-07-27 11:04:34

2012-05-28 10:04:34 2012-07-22 09:04:34**

WHERE  RN_CREATE_DATE=strftime('%Y', '2012-07-28 12:04:34')

but no result found, just help me out with this.


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.


  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

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:

    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:

    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:

    insert into Event 
        (name, ratingOutOf10, numberOfRatings, category, venue, fromDateTime, description, pricesList, termsAndConditions)
        ('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)
        ('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.

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.

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

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

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:

WHERE  RN_CREATE_DATE='2012-07-28 12:04:34'

Need Your Help

start index at 1 for Pandas DataFrame

python pandas csv dataframe indexing

I need the index to start at 1 rather than 0 when writing a Pandas DataFrame to CSV.

Git: How to revert 2 files that are stubbornly stuck at "Changed but not committed"?

git github revert

I have a repo that has two files that supposedly I changed locally.