SQL Date Functions/ 数据库中日期时间函数

来源:互联网 发布:班班通软件下载 编辑:程序博客网 时间:2024/05/18 04:30

原文来自:http://www.w3schools.com/sql/sql_dates.asp

SQL Dates

Note 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.


MySQL Date Functions

The following table lists the most important built-in date functions in MySQL:

FunctionDescriptionNOW()Returns the current date and timeCURDATE()Returns the current dateCURTIME()Returns the current timeDATE()Extracts the date part of a date or date/time expressionEXTRACT()Returns a single part of a date/timeDATE_ADD()Adds a specified time interval to a dateDATE_SUB()Subtracts a specified time interval from a dateDATEDIFF()Returns the number of days between two datesDATE_FORMAT()Displays date/time data in different formats

SQL Server Date Functions

The following table lists the most important built-in date functions in SQL Server:

FunctionDescriptionGETDATE()Returns the current date and timeDATEPART()Returns a single part of a date/timeDATEADD()Adds or subtracts a specified time interval from a dateDATEDIFF()Returns the time between two datesCONVERT()Displays date/time data in different formats

SQL Date Data Types

MySQL comes with the following data types for storing a date or a date/time value in the database:

  • 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 Server comes with the following data types for storing a date or a date/time value in the database:

  • DATE - format YYYY-MM-DD
  • DATETIME - format: YYYY-MM-DD HH:MM:SS
  • SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
  • TIMESTAMP - format: a unique number

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.


SQL Working with Dates

Note You can compare two dates easily if there is no time component involved!

Assume we have the following "Orders" table:

OrderIdProductNameOrderDate1Geitost2008-11-112Camembert Pierrot2008-11-093Mozzarella di Giovanni2008-11-114Mascarpone Fabioli2008-10-29

Now we want to select the records with an OrderDate of "2008-11-11" from the table above.

We use the following SELECT statement:

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

The result-set will look like this:

OrderIdProductNameOrderDate1Geitost2008-11-113Mozzarella di Giovanni2008-11-11

Now, assume that the "Orders" table looks like this (notice the time component in the "OrderDate" column):

OrderIdProductNameOrderDate1Geitost2008-11-11 13:23:442Camembert Pierrot2008-11-09 15:45:213Mozzarella di Giovanni2008-11-11 11:12:014Mascarpone Fabioli2008-10-29 14:56:59

If we use the same SELECT statement as above:

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

we will get no result! This is because the query is looking only for dates with no time portion.

Tip: If you want to keep your queries simple and easy to maintain, do not allow time components in your dates!


原创粉丝点击