SQL DateFunctions
SQL Dates
When dealing with dates, the hardest task is probably ensuring that the format of the inserted date matches the format of the date column in the database.
As long as your data contains only the date part, running queries will not be a problem. However, if the time part is involved, the situation is a bit more complicated.
Before discussing the complexity of date queries, let's first look at the most important built-in date processing functions.
MySQL Date Functions
The following table lists the most important built-in date functions in MySQL:
| Function | Description |
|---|---|
| NOW() | Returns the current date and time |
| CURDATE() | Returns the current date |
| CURTIME() | Returns the current time |
| DATE() | Extracts the date part of a date or date/time expression |
| EXTRACT() | Returns the individual part of a date/time |
| DATE_ADD() | Adds a specified time interval to a date |
| DATE_SUB() | Subtracts a specified time interval from a date |
| DATEDIFF() | Returns the number of days between two dates |
| DATE_FORMAT() | Displays date/time in different formats |
SQL Server Date Functions
The following table lists the most important built-in date functions in SQL Server:
| Function | Description |
|---|---|
| GETDATE() | Returns the current date and time |
| DATEPART() | Returns the individual part of a date/time |
| DATEADD() | Adds or subtracts a specified time interval from a date |
| DATEDIFF() | Returns the time between two dates |
| CONVERT() | Displays date/time in different formats |
SQL Date Data Types
MySQLStore date or date/time values in a database using the following data types:
- DATE - Format: YYYY-MM-DD
- DATETIME - Format: YYYY-MM-DD HH:MM:SS
- TIMESTAMP - Format: YYYY-MM-DD HH:MM:SS
- YEAR - Format: YYYY or YY
SQL ServerStore date or date/time values in a database using the following data types:
- DATE - Format: YYYY-MM-DD
- DATETIME - Format: YYYY-MM-DD HH:MM:SS
- SMALLDATETIME - Format: YYYY-MM-DD HH:MM:SS
- TIMESTAMP - Format: Unique number
Note:When you create a new table in the database, you need to select a data type for the columns!
For all available data types, visit our completeData Types Reference Manual。
SQL Date Processing
If the time part is not involved, we can easily compare two dates!
Suppose we have the following "Orders" table:
| OrderId | ProductName | OrderDate |
|---|---|---|
| 1 | Geitost | 2008-11-11 |
| 2 | Camembert Pierrot | 2008-11-09 |
| 3 | Mozzarella di Giovanni | 2008-11-11 |
| 4 | Mascarpone Fabioli | 2008-10-29 |
Now, we want to select the record with OrderDate "2008-11-11" from the table above.
We use the following SELECT statement:
The result set is shown below:
| OrderId | ProductName | OrderDate |
|---|---|---|
| 1 | Geitost | 2008-11-11 |
| 3 | Mozzarella di Giovanni | 2008-11-11 |
Now, suppose the "Orders" table looks like this (note the time part in the "OrderDate" column):
| OrderId | ProductName | OrderDate |
|---|---|---|
| 1 | Geitost | 2008-11-11 13:23:44 |
| 2 | Camembert Pierrot | 2008-11-09 15:45:21 |
| 3 | Mozzarella di Giovanni | 2008-11-11 11:12:01 |
| 4 | Mascarpone Fabioli | 2008-10-29 14:56:59 |
If we use the same SELECT statement as above:
SELECT * FROM Orders WHERE OrderDate='2008-11-11' 或 SELECT * FROM Orders WHERE OrderDate='2008-11-11 00:00:00'
Then we will not get any results! Because there is no date "2008-11-11 00:00:00" in the table. Without a time part, the default time is 00:00:00.
Tip:If you want to keep queries simple and easier to maintain, please do not use the time part in dates!
Other Extensions