SQL Date function

Source: Internet
Author: User

SqlDate function SQL Date (Dates)

When we work on dates, the hardest task is to ensure that the date you insert is formatted to match the format of the date column in the database.

As long as your data contains only the date parts, running the query will not be a problem. However, if the time part is involved, the situation is a bit complicated.

Before we discuss the complexity of the date query, let's look at the most important built-in date processing functions.

MySQL Date function

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 () Extract date part of date or date/time expression
EXTRACT () Returns a separate part of the date/time
Date_add () Add a specified time interval to a date
Date_sub () Subtract a specified time interval from a date
DATEDIFF () Returns the number of days between two dates
Date_format () Display Date/time in a different format

SQL Server Date function

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 separate part of the date/time
DATEADD () Add or subtract a specified time interval in a date
DATEDIFF () Returns a time between two dates
CONVERT () Display Date/time in a different format

SQL Date Data type

MySQL Stores date or date/time values in the 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 Server uses the following data types to store date or date/time values 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: Unique number

Note: When you create a new table in the database, you need to select a data type for the column!

For information on all available data types, please visit our full data type reference manual.

SQL Date Processing

If the time part is not involved, then 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 a record from the table above that OrderDate is "2008-11-11".

We use the following SELECT statement:

SELECT *  from WHERE OrderDate='2008-11-11'

The result set is as follows:

OrderId ProductName OrderDate
1 Geitost 2008-11-11
3 Mozzarella di Giovanni 2008-11-11

Now, let's say the Orders table looks like this (note the time section 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 WHERE OrderDate='2008-11-11'

Or
SELECT *  from WHERE OrderDate='2008-11-11 00:00:00'

Then we will not get the result! Because there is no "2008-11-11 00:00:00" date in the table. If there is no time section, the default time is 00:00:00.

Tip: If you want to make queries simple and easier to maintain, don't use the time section in dates!

SQL Date function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.