Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,810 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);