From my StackOverflow answer on the subject of parsing SQLite string dates:

Python makes it pretty easy to add a user-defined function to a SQLite query. Let’s demonstrate this by populating a table full of text reprensentations of dates, and then use a simple Python UDF we write here called date_parse to attempt to parse the text date into a real date.

First, we need to do some basic setup. Let’s do some standard Python imports firsts. We’ll use Python’s build-in SQLite support, as well as it’s built-in date parser. We also have ensured pip install pandas was run so that we can use Pandas to display our demo output nicely in Jupyter Lab.

#!/usr/bin/env python3
import sqlite3
from dateutil import parser
import pandas as pd

Now, let’s make a populate_db function that will take a connection to a SQLite database with a table called txtdates containing various text reprensentations of dates.

def populate_db(con):
    ''' Setup some values to parse '''
    cur = con.cursor()

    # Make a table
    sql = '''
    CREATE TABLE txtdates (
        "id" integer primary key,
        "date" text
    );
    '''
    cur.execute(sql)

    # Fill the table
    dates = [
        '2/1/03',
        '03/2/04',
        '4/03/05',
        '05/04/06',
        '6/5/2007',
        '07/6/2008',
        '8/07/2009',
        '09/08/2010',
        '2-1-03',
        '03-2-04',
        '4-03-05',
        '05-04-06',
        '6-5-2007',
        '07-6-2008',
        '8-07-2009',
        '09-08-2010',
        '31/12/20',
        '31-12-2020',
        'BOMB!',
        None
    ]
    params = [(x,) for x in dates]
    cur.executemany(''' INSERT INTO txtdates ("date") VALUES (?); ''', params)

Now, we’re ready to make our User-Defined Function (UDF) called date_parse.

def date_parse(s):
    ''' SQL UDF to convert a string to a date '''
    try:
        t = parser.parse(s, parser.parserinfo(dayfirst=True))
        return t.strftime('%Y-%m-%d')
    except:
        return None

And finally, let’s put it all together. Create an in-memory SQLite database, populate the database with our test date data, add our Python UDF, and query the parsed results.

# Create an in-memory SQLite database
with sqlite3.connect(":memory:") as con:
    # Initialize the database
    populate_db(con)

    # Tell SQLite about our Python UDF
    con.create_function("date_parse", 1, date_parse)

    # Construct a SQL SELECT
    query = '''
      SELECT "id"
           , "date"
           , date_parse("date") as parsed
        FROM txtdates
    ORDER BY 3
    ;
    '''

    # Pull and display the data
    df = pd.read_sql(query, con=con)

# Show results
df
iddateparsed
12/1/032003-01-02
203/2/042004-02-03
34/03/052005-03-04
405/04/062006-04-05
56/5/20072007-05-06
607/6/20082008-06-07
78/07/20092009-07-08
809/08/20102010-08-09
92-1-032003-01-02
1003-2-042004-02-03
114-03-052005-03-04
1205-04-062006-04-05
136-5-20072007-05-06
1407-6-20082008-06-07
158-07-20092009-07-08
1609-08-20102010-08-09
1731/12/202020-12-31
1831-12-20202020-12-31
19BOMB!None
20NoneNone

Now we can see how all the parsable date forms came out in YYYY-MM-DD form. This isn’t something that SQLite has an easy built-in way to do, so using a Python UDF saves the day!