T_sql Date function

Source: Internet
Author: User
Tags date1

  

The date and time of the date function cardinality expression or the return value from the time interval.

GETDATE (), returns the date and time of the current system. Cases:

SELECT GETDATE (); The result: 2010-05-18 15:53:08.920

DATEPART (Item,date), returns the date specified by the item (Year,month,day,,weekday,hour,minute,secound, etc.), or the corresponding shorthand (year:y), returning the result to an integer. Cases:

SELECT DATEPART (year, ' 2010-05-18 '); The result is: 2010

SELECT DATEPART (MONTH, ' 2010-05-18 '); The result is: 5

SELECT DATEPART (Day, ' 2010-05-18 '); The result is: 18

SELECT DATEPART WEEKDAY, ' 2010-05-18 '); Results: 3 (Tuesday)

SELECT DATEPART (HOUR, ' 2010-05-18 15:59:30 '); The result is: 15

SELECT DATEPART (MINUTE, ' 2010-05-18 15:59:30 '); The result is: 59

SELECT DATEPART (SECOND, ' 2010-05-18 15:59:30 '); The result is: 30

Datename (Item,date), returns the specified item,date of the date as a string. Cases:

SELECT datename (WEEKDAY, ' 2010-05-18 '); Results were: Tuesday

DATEDIFF (ITEM,DATE1,DATE2), calculates the difference between two date parts date1 and Date2, and returns the result of item representing the integer value of the cell. Cases:

SELECT DATEDIFF (year, ' 2000-1-1 ', ' 2010-05-18 '); Result is 10

SELECT DATEDIFF (year, ' 2010-05-18 ', ' 2000-1-1 '); Result is-10

SELECT DATEDIFF (MONTH, ' 2010-01-01 ', ' 2010-05-18 '); Result is 4

SELECT DATEDIFF (Day, ' 2010-05-01 ', ' 2010-05-18 '); Result is 17

DATEADD (i,n,d), the number n in the I-value unit is increased to the specified date d. The result is a datetime type. Cases:

SELECT DATEADD (day,7, ' 2010-05-18 '); The result: 2010-05-25 00:00:00.000

SELECT DATEADD (year,-5, ' 2010-05-18 '); The result: 2005-05-18 00:00:00.000

T_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.