Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Date Functions in SQL Server and MySQL

SQL Date Functions


SQL Dates

NoteWhen working with dates, perhaps the hardest task is ensuring that the format of the date being inserted matches the format of the date column in the database.

As long as your data contains only the date part, queries will run without problems. However, if the time part is involved, things get a bit more complicated.

Before discussing the complexities of date queries, let's first look at the most important built-in date handling 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 a single 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 a 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 a single 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 a date/time in different formats

SQL Date data types

MySQL Use the following data types to store date or date/time values in a database:

SQL Server Use the following data types to store date or date/time values in a database:

**Note:** When you create a new table in a database, you need to choose a data type for each column!

To learn about all available data types, visit our complete Data Types Reference Manual。


SQL Date Handling

NoteIf 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 records from the table above where OrderDate is "2008-11-11".

We use the following SELECT statement:

SELECT * FROM Orders WHERE OrderDate='2008-11-11'

The result set looks like this:

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 get no results! Because there is no "2008-11-11 00:00:00" date in the table. If there is no time part, the default time is 00:00:00.

**Tip:** If you want to keep queries simple and easier to maintain, do not use the time part in dates!