SQL statement to convert date format to some columns

I have a SQLite database in which some of the columns are date and time strings with 1/2/2000 format at 3:45:56 AM.

Is there an SQL statement that can convert these values ​​to strings with an ISO-8601-like format (2000-01-02 03:45:56)?

(One possible way, other than SQL, could be to export the database as a CSV file and use Python with its csv and datetime modules to analyze the columns.)

+3
source share
2 answers

: strftime. , ISO 8601 ( Unix).

SQL , . - .

def fix_timestamp(timestamp):
    return datetime.datetime.strptime(timestamp, '%m/%d/%Y %I:%M:%S %p') \
           .strftime('%Y-%m-%d %H:%M:%S')

db = sqlite3.connect('...')
db.create_function('FixTimestamp', 1, fix_timestamp)

UPDATE TheTable SET TheColumn = FixTimestamp(TheColumn).

0

Source: https://habr.com/ru/post/1781374/


All Articles