SQL Server datediff function usage

Source: Internet
Author: User
Tags current time getdate

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

Syntax

The startdate AND enddate parameters of DATEDIFF (datepart, startdate, enddate) are valid date expressions.

Use the following SELECT statement:

The code is as follows: Copy code

Select datediff (day, '2017-12-29 ', '2017-12-30') AS DiffDate result:

DiffDate

1

Use datediff to obtain the timestamp

The code is as follows: Copy code

1> SELECT datediff (ss, '2017-01-01 ', GETDATE () as time
2> go
Time
1361988498
(1 row affected)
1>

Datediff

Ss indicates in seconds

The timestamp is calculated from this date.

Getdate () current time

The number of seconds between the current read time and is the current timestamp.

Of course, if you can use getdate () to read the current date, you can also use other languages to convert it into a timestamp, such as js and php.

Of course, the datediff function is very powerful, for example, to calculate the number of months between two dates

The code is as follows: Copy code

1> SELECT datediff (mm, '2017-01-01 ', GETDATE () as time
2> go
Time
13
(1 row affected)

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.