In the context of this exercise, what does the strftime() function do?
Answer
The strftime() function is used to format date type values, and allows us to do useful things such as extract individual values like the month, year, or day from the date value.
For example, we can obtain these values like so, from some date.
/* Returns the day of month */
strftime("%d", date)
/* Returns the month */
strftime("%m", date)
/* Returns the year */
strftime("%Y", date)
SQLite comes with a strftime() function - a very powerful function that allows you to return a formatted date.
It takes two arguments:
strftime(format, column)
For strftime(__, timestamp) :
* `%Y` returns the year (YYYY)
* `%m` returns the month (01-12)
* `%d` returns the day of the month (1-31)
* `%H` returns 24-hour clock (00-23)
* `%M` returns the minute (00-59)
* `%S` returns the seconds (00-59)
if `timestamp` format is `YYYY-MM-DD HH:MM:SS` .
WITH january AS (
SELECT *
FROM plays
WHERE strftime("%m", play_date) = '01'
),
february AS (
SELECT *
FROM plays
WHERE strftime("%m", play_date) = '02'
)
There were two temporary tables created called january and february.
Does anyone know how to get the query to run correctly? I had input the following: SPOILER
which should be literally the same thing as the solution, but whenever i run the query, it brings up nothing…not sure if it’s me or a glitch or something
In your last select statement, try specifying the table the user_id should be selected. It could be from january.user_id or february.user_id.
WITH january AS (
SELECT *
FROM plays
WHERE strftime("%m", play_date) = '01'
),
february AS (
SELECT *
FROM plays
WHERE strftime("%m", play_date) = '02'
)
SELECT january.user_id -- prefix user_id with table name
FROM january
LEFT JOIN february
ON january.user_id = february.user_id
WHERE february.user_id IS NULL;
@smlaroo7178396734 reviews_2020 is created as a temporary table to refine the 2020 data from the reviews table - you would then have to specify that you were using that review_2020 table in the second half of the code (instead of the reviews table) which would then return results from that 2020 specific data.
Recall seeing that the default date format in SQL is YYYY-mm-dd, which might explain why I get null values (as dates are inputted as dd-mm-YYYY, and sometimes as dd-m-YYYY). However, the orders ‘database schema’ does list the variable type as DATE.
Any way to extract year, or am I missing something in my query?