SQL Server common time-processing functions

Source: Internet
Author: User
Tags time interval

1. Get the current time

SELECT GetDate ();

2. Get time in separate years, months, days and hours, minutes, seconds

Year (GetDate ()) part of the current time

Month (GetDate ()) monthly part of the current time

Day part of the current time (GetDate ())

The above function, in turn, returns the value of type int, which returns nvarchar

Datename (year, GetDate ())

Datename (MONTH, GetDate ())

Datename (Day, GetDate ())

3. Calculate a time interval of two date types

DATEDIFF (year, ' 2013-01-01 ', ' 2014-01-01 ') returns 1

DATEDIFF (MONTH, ' 2013-01-01 ', ' 2014-01-01 ') returns 12

DATEDIFF (Day, ' 2013-01-01 ', ' 2014-01-01 ') returns 365

4. Time plus minus

DATEADD (Day, GETDATE ()) 15 days ago

DATEADD ( -15, Day, GETDATE ()) 15 days later

5. Time interception

CONVERT (varchar), GETDATE (), 1) return 05/16/06

The significance of the specific elaboration is here: http://msdn.microsoft.com/zh-cn/library/ms187928%28v=sql.100%29.aspx

6. Determine the day of the week

Select DATEPART (dw,getdate ())-1

7. Judge the first quarter of the day

(MONTH (GetDate ())-1)/4 +1

SQL Server common time-processing functions

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.