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

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
216,057 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
216,057 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
216,057 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.