shafty
shafty

Reputation: 605

Python/SQLite sorting by TEXT date

The SQLite documentation says to store dates as TEXT or as sqlite binaries. So, I thought I'd use str(datetime.datetime.now()). How would you go about sorting a column by date order when it's stored in TEXT format?

Upvotes: 1

Views: 230

Answers (2)

msw
msw

Reputation: 43527

Only ISO 8601 Time Format has any chance of being sortable as text and even that's chancy as there are are multiple legal formats.

This is why the datetime module exists, use it.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124170

ISO dates are text sortable:

>>> datetime.now().isoformat()
'2012-06-16T19:34:05.418407'
>>> sorted([datetime.now().isoformat(), (datetime.now() - timedelta(days=100)).isoformat()])
['2012-03-08T19:34:06.802304', '2012-06-16T19:34:06.802288']

Upvotes: 3

Related Questions