Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Reporting with SQL Date and Time Functions Practice Session

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
231,268 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.

Thanks Steven!

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);