r/programming • • Apr 28 '23

SQLite is not a toy database

https://antonz.org/sqlite-is-not-a-toy-database/
305 Upvotes

179 comments sorted by

View all comments

Show parent comments

62

u/maxinstuff Apr 29 '23

If you use ISO compliant datetime strings they’ll sort correctly as strings too 🤓

0

u/[deleted] Apr 29 '23

[deleted]

33

u/maxinstuff Apr 29 '23

Classic programmers right?

“This will be a problem one day but I sure won’t be here for it!”

19

u/[deleted] Apr 29 '23

To be fair, most datetime fields don't support year 10000 either.

4

u/[deleted] Apr 29 '23

I may not live to see what is to come in 10000 years!

8

u/[deleted] Apr 29 '23

At the rate humanity is going it’s doubtful anyone will be around for it.

3

u/CalebAsimov Apr 29 '23

Future post-apacolyptic data archaeologists: "We've found a timestamp of the end of the world! But we're not sure which 10,000 year epoch it was in."

2

u/maxinstuff Apr 30 '23

I like to imagine some Aztecs once having this exact conversation 🤣

9

u/User31441 Apr 29 '23

True. I just find integers easier to work with. Wanna span over a range? Just add some number to the start date. With ISO strings I now gotta worry about whether or not the end of the month was reached. Still okay to work with (because I can just convert to a date type in the program, do my calculations and then stringify again) but the numbers are more straightforward.

21

u/[deleted] Apr 29 '23

You can do plenty of those calculations in sqlite itself. Check out the examples there.

sqlite> SELECT datetime('now');
2023-04-29 03:54:59

sqlite> SELECT datetime('now', '+3 months');
2023-07-29 03:55:03

sqlite> CREATE TABLE foo(date TEXT NOT NULL);

sqlite> INSERT INTO foo (date) VALUES (datetime('now')), (datetime('now', '+3 months')), (datetime('now', '-3 months'));

sqlite> SELECT date FROM foo;
2023-04-29 03:55:33
2023-07-29 03:55:33
2023-01-29 03:55:33

sqlite> SELECT datetime(date, '+1 month') FROM foo;
2023-05-29 03:55:33
2023-08-29 03:55:33
2023-03-01 03:55:33

Personally, I still just use integers anyway. Stores smaller, easier to reason about, and usually it's easier to work with timestamps to convert between the database and the language's preferred datetime representation.