About SQL Server database date functions

Source: Internet
Author: User
Tags time interval

About SQL Server database date functions

1. Get current time Select GETDATE ()

2, intercept the required value

Select DATEPART (year,getdate ())

Select DATEPART (month,getdate ())

Select DATEPART (day,getdate ())

Select DATEPART (hour,getdate ())

Select DATEPART (minute,getdate ())

Select DATEPART (second,getdate ())

Select DATEPART (week,getdate ())

3. Add or subtract a specified time interval from a date

Select DATEADD (Year,3,getdate ())--Get the current time and defer three years in the future

Select DATEADD (Month,3,getdate ())--Get the current time and defer three months later

Select DATEADD (Day,3,getdate ())--Get the current time and defer three days in the future

Select DATEADD (Hour,3,getdate ())--Get the current time and defer three hours later

Select DATEADD (Minute,3,getdate ())--Get the current time and defer three minutes later

Select DATEADD (Second,3,getdate ())--Get the current time and defer three seconds later

4. Returns the time between two dates

Select DateDiff (year, ' 2001-08-19 ', GETDATE ())--2001-08-19 and current time difference between the young

Number of months between select DateDiff (month, ' 2001-08-19 ', GETDATE ())--2001-08-19 and current time

Select DateDiff (Day, ' 2001-08-19 ', GETDATE ()) How many days difference between--2001-08-19 and current time

5. Display date/time in different format

Select CONVERT (Char,getdate (), 8)--show current time-minute-second

Select CONVERT (Char,getdate (), 10)--show current month-day-year, display form "08-19-11"

Select CONVERT (Char,getdate (), 11)--show current year-month-day, display form "11/08/19"

Select CONVERT (Char,getdate (), 14)--show current time-minutes-seconds-milliseconds, display form "14:54:57:090"

Note: Parameter 1-14 is optional.

About SQL Server database date 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.