The most difficult part when working with dates is to be sure that the format of the date you are trying to insert, matches the format of the date column in the database.
As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets complicated.
Before talking about the complications of querying for dates, we will look at the most important built-in functions for working with dates.
The Below table lists the most important built-in date functions in MySQL:
The Below table lists the most important built-in date functions in SQL Server:
MySQL comes with the following data types for storing a date or a date/time value in the database:
SQL Server comes with the following data types for storing a date or a date/time value in the database:
Note: The date types are chosen for a column when you create a new table in your database
For an overview of all data types available, go to our complete Data Types reference.
You can compare two dates easily if there is no time component involved
Assume we have the following "Purchases" table:
Now we want to select the records with an OrderDate of "2015-11-11" from the table above.
We use the following SELECT statement:
The result-set will look like this:
Now, assume that the "Purchases" table looks like this (notice the time component in the "OrderDate" column):
If we use the same SELECT statement as above:
we will get no result! This is because the query is looking only for dates with no time portion.
Hint: If you want to keep your queries simple and easy to maintain, do not allow time components in your dates
Your Query was successfully sent!