SQL Server time and date functions

Source: Internet
Author: User
Tags month name time and date

1. dateadd
Syntax
Dateadd (datepart, number, date)
Parameters
Datapart specifies the date component of the new value to be returned.
Commonly used: Year, quarter, month, dayofyear, day, week, weekday
Number is used to add a value to datepart. If a non-integer value is specified, the fractional part of the value is discarded.
Date expression, which is used to return datetime, smalldatetime, or a string in date format.
Return type
Returns datetime. However, if the date parameter is smalldatetime, smalldatetime is returned.
Example
The following example will output a list of order time ranges in the adventureworks database. The time range is the date of the current order plus 21 days.

1 Use Adventureworks;
2 Go
3 Select   Dateadd ( Day , 21 , Orderdate) As Timeframe
4 From Sales. salesorderheader;
5 Go
6
7 Select   Dateadd ( Day , 1 , Getdate ())

 

2. datediff
Syntax
Datediff (datepart, startdate, enddate)
Remarks
Enddate minus startdate. If startdate is later than enddate, a negative value is returned.
Parameters
Datepart
Startdate
Start date of the calculation. Startdate is an expression that returns the datetime, smalldatetime, or date format string.
Enddate
End date of the calculation. Enddate is an expression that returns the datetime, smalldatetime, or date format string.
Return type
Integer

Example:

1 Use Adventureworks;
2 Go
3 Select   Datediff ( Day , Orderdate, Getdate ()) As Numberofdays
4 From Sales. salesorderheader;
5 Go
6
7 Select   Datediff ( Day , ' 2010-8-1 ' , Getdate ())

 

3. datename
Returns the string that represents the specified date part of the specified date.

Syntax
Datename (datepart, date)

Parameters
Datepart
Is a parameter that specifies the date part to be returned.
Date
Returns the datetime or smalldatetime value or a string in the date format.

Return type
Nvarchar

Example

1 Select   Datename ( Month , Getdate ()) As   ' Month name '

 

 

4. datepart
Returns an integer that represents the specified date of a specified date.

Syntax
Datepart (datepart, date)

Remarks
The day, month, and year functions are synonyms of datepart (DD, date), datepart (mm, date), and datepart (YY, date.

Parameters
Datepart
Specify the parameters of the date to be returned.
Date
Returns a string of datetime, smalldatetime, or date format.

Return type
Int

Example

1 Select   Getdate () As   ' Current date '
2 Go

 

 

 

Related Article

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.