From 9df590f855c47c43e772bc3a566c366ebd3194c1 Mon Sep 17 00:00:00 2001 From: Dave Morriss Date: Thu, 12 Oct 2023 18:37:01 +0100 Subject: [PATCH] Added a SQLite section to query_next_available_episode.md --- query_next_available_episode.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/query_next_available_episode.md b/query_next_available_episode.md index 2d24a58..cbcc7e8 100644 --- a/query_next_available_episode.md +++ b/query_next_available_episode.md @@ -1,4 +1,4 @@ -# Documentation of the `query query_next_available_episode` +# Documentation of the query `query_next_available_episode` ## Purpose @@ -153,6 +153,33 @@ Using the full query against the database on 2023-10-12 we get the following res +---------+------------+------------+ ``` +## Differences in the SQLite version + +1. Many of the functions used take string arguments, and since the query is + stored in a single-quoted string, these have to be escaped with a backslash. + +2. The SQLite function to get the current date is `date('now')` + +3. The SQLite function to get the day of the week from a date is + `strftime('%w',last_date)` and the values are 0 for Sunday and 6 for Saturday. + +4. When adding days to a date in SQLite the function is `date(date,'+3 days')` + and this is why such strings are returned from the `CASE` statement. + +5. To compute the difference in days between two dates in SQLite it is + necessary to convert dates to Julian dates and subtract the values. This can + give a negative result, so we use `abs()` to prevent it. The result is + fractional because of the way Julian dates are implemented in SQLite, so + `printf('%i,?)` is used to get the integer part. Therefore, the following + expression is needed: +``` + printf('%i', + abs( + julianday(DATE (prev_date, date_offset)) - + julianday('now') + 1) + ) AS delta_days +``` +