Databases Reporting with SQL Date and Time Functions Practice Session

Tony Pitkin
Tony Pitkin
Pro Student 1,950 Points

Formatting Dates in a simple way?

From the practice session:

Format dates in all the loans table in the UK format without the year. For example, April 1st is 01/04.

I came up with the following:

SELECT id, book_id, patron_id, STRFTIME("%m/%d", loaned_on) AS "loaned_on", STRFTIME("%m/%d", return_by) AS "return_by", STRFTIME("%m/%d", returned_on) AS "returned_by" FROM loans

My question: Is there a way to make the above more efficient?

3 Answers

Steven Parker
Steven Parker
203,238 Points

That's pretty close, unless you interpret the instructions to imply "return only the dates...", or to modify the dates in the table instead of just showing them.

Otherwise, the only issue is that they asked for the "UK format" which puts the day before the month.

Your format is a little off, it should be dd/mm. For a simpler interpretation of what they were asking here are only the dates without aliases.

SELECT STRFTIME("%d/%m", loaned_on), STRFTIME("%d/%m", return_by), STRFTIME("%d/%m", returned_on) FROM loans;
Anshul Laikar
Anshul Laikar
4,428 Points

This is what I came up with, in case they wanted us to actually replace the values given to us:

UPDATE loans 
SET loaned_on = STRFTIME("%d/%m", loaned_on), return_by = STRFTIME("%d/%m", return_by), returned_on = STRFTIME("%d/%m", returned_on);