SQL Date function

Source: Internet
Author: User

SQL Date Data type

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

Use the following SELECT statement:

SELECT GETDATE () as Currentdatetime



The DATEPART () function is used to return a separate part of a date/time, such as year, month, day, hour, minute, and so on.

Grammar
DATEPART (DATEPART,date)

The DATEADD () function adds or subtracts a specified time interval from a date.

Grammar
DATEADD (datepart,number,date)

The date parameter is a valid day expression. The number is the number of intervals you want to add, and for the future time, this is positive, and for the past time, this number is negative.

The datepart parameter can be the following value:

The DATEDIFF () function returns the number of days between two dates.

Grammar
DATEDIFF (datepart,startdate,enddate)

The startdate and enddate parameters are valid date expressions.

The convert () function is a general function that converts a date to a new data type.

The CONVERT () function can display date/time data in different formats.

Grammar
CONVERT (data_type (length),data_to_be_converted,style)

data_type (length) Specifies the target data type (with optional lengths). The data_to_be_converted contains values that need to be converted. style Specifies the date/time output format.

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.