SQL dateadd (TRANSACT-SQL) returns the time as needed,

Source: Internet
Author: User

 

1. Syntax:

 

 
Dateadd (datepart, number, date)

2. Parameters
Datepart

Yes andIntegerThe date part of the Number Addition. The following table lists all valid datepart parameters. The user-defined equivalent variable is invalid.

Datepart

Abbreviations

Year

YY,Yyyy

Quarter

Qq,Q

Month

Mm,M

Dayofyear

Dy,Y

Day

Dd,D

Week

WK,WW

Weekday

DW,W

Hour

HH

Minute

Mi,N

Second

SS,S

Millisecond

MS

Microsecond

MCS

Nanosecond

NS

Number

Is an expression that can be parsed into an int that is added to datepart of date. User-Defined variables are valid.

If you specify a decimal value, the decimal number is truncated without rounding.

Date

Is an expression that can be parsedTime,Date,Smalldatetime,Datetime,Datetime2OrDatetimeoffsetValue. Date can be an expression, column expression, user-defined variable, or string text. If the expression is a string, it must be resolved toDatetimeValue. To avoid uncertainty, use a four-digit year. For information about the two-digit year, see the two digit year cutoff option.

 

 

 

 

3. Example: return the order of the last three months. Consider the specific date.

View code 1 Select * From orderst Where Adddate > Dateadd (m, - 3 , Getdate () Order by adddate

 

 

 

Return the order of the first three months. If the date is not considered, it starts from the first day.

 

CodeAs follows:

View code 1 Select * From orderst Where Datediff (M, adddate, getdate ()) <= 3 Order by adddate

 

 

 

4. display today's order

Select * From orderst where adddate> convert (varchar, getdate (), 112) order by adddate

 

 

 

 

 

 

 

 

1. Refer to: msdn -- dataadd

 

 

 

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.