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

General Discussion

Databases: Datatype Question

In the intro to databases vid, it is mentioned that when for an an event in stored as a varchar, and is later sorted, there will be some errors. Can someone do deeper into this, just curious .

S

2 Answers

James Barnett
James Barnett
39,199 Points

shamim khaled - We can certainly help you with that, if you can post a link to the video you are referring to.

Anthony Garritano
PLUS
Anthony Garritano
Courses Plus Student 2,165 Points

I believe you're referencing the Introduction to Data Types video. At about 3:12 Andrew explains:

It's important to set the appropriate data type for the data you'll be storing in your database. That is, if a piece of data is a date and time, you'd want to store it with the type DATETIME. The reason for this is that the database is clued in as to how to treat the data; it knows that a date is a date and not just a random series of numbers.

Let me expound with Andrew's example from the video:

If the datatype is set to DATETIME and you sort it, it knows which months are which and what portion of the data is time, thus it sorts correctly: Screenshot of data set to type DATETIME

If the datatype is set to VARCHAR and you sort it, it's sorted alphanumerically, which isn't quite what you would have been expecting: Screenshot of data set to type VARCHAR. The months are out of order!!

If did store a date as VARCHAR, you won't get an error in that sense that the database detects something wrong, it's when you go to use your data that you discover things aren't working the way you intended.