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

Date Formatting for all date columns

-- The loans table has the following ids columns: id, book_id, patron_id; and the following date columns: loaned_on, return_by, returned_on; -- Format dates in all the loans table in the UK format without the year. For example, April 1st is 01/04.

My question is about modifying dates for all columns without using the STRFTIME function which only adds another column.

How would you format dates for all columns without using the STRFTIME column to complete the above objective?

Steven Parker
Steven Parker
231,072 Points

This might be possible in other database engines, but I don't think SQLite provides a way to customize the default date format. You just use strftime on each column you wish to display in a special format.

3 Answers

Steven Parker
Steven Parker
231,072 Points

Are you asking about how to format dates?

There's a function for doing that, called strftime. The first argument is a string that defines the format the date will be displayed with, and the second argument is the date to be formatted.

See the SQLite documentation for a list of the possible formatting codes.

Steven Parker
Steven Parker
231,072 Points

You can have functions on multiple columns, and you can nest functions inside each other (use one as an argument for another) when needed.

Alright it worked. Thank you. I was burnt yesterday when I was trying this.

SELECT STRFTIME("%d/%m", loaned_on) AS uk_loaned_on, STRFTIME("%d/%m", return_by) AS uk_return_by, STRFTIME("%d/%M", returned_on) AS uk_returned_on FROM loans;

I was trying all 3 columns in ONLY one STRFTIME function yesterday. I didn't know you could put in more than one STRFTIME function.