DATEDIFF (Transact-SQL)

Source: Internet
Author: User
Tags time interval

Datadiff is used to get datasets within a specified time interval, such as:

                SelectCount (1) ascol2 fromComplaintmain cmwhereDateDiff (dd,cm. Createdon,getdate ())<=7and cm. Handlingby="+ (int) HANDLINGBY.MEMBERCD + @"and cm. Cstate="+ (int) complaintstate.successfully +";";

The above query is to get the condition data for one week;

Function method:

DATEDIFF (DatePart, StartDate, EndDate)

DatePart Parameters:

DatePart

Abbreviations

Year

YY, yyyy

Quarter

QQ, Q

Month

MM, M

DayOfYear

Dy, y

Day

DD, D

Week

WK, WW

Hour

hh

Minute

MI, n

Second

SS, S

Millisecond

Ms

Microsecond

MCs

Nanosecond

Ns

Demo

SELECT DATEDIFF (year,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Quarter,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Month,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (DayOfYear,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Day,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Week,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Hour,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (minute,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Second,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000'); SELECT DATEDIFF (Millisecond,'2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

Example:

CREATE TABLE dbo. Duration    (    startdate datetime2    , endDate datetime2    ); INSERT into dbo. Duration (startdate,enddate)    VALUES ('2007-05-06 12:10:09','  2007-05-07 12:10:09'Duration'fromdbo. Duration; 1

DATEDIFF (Transact-SQL)

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.