SQL time and date addition and subtraction Functions

Source: Internet
Author: User
This article describes some common time and date processing functions in SQL, including the-DATEPARTDATEDIFFCONVERTgetdate () DATEADD functions.

This article describes some commonly used time and date processing functions in SQL, including the-DATEPART DATEDIFF CONVERT getdate () DATEADD functions.

-- Get the current time
Select getdate ()
-- Obtain the current year, month, and day. YY indicates the year, MM indicates the month, DD indicates the day, hh indicates the day, and ss indicates the second.
/*

The Code is as follows:

Year yy 1753--9999 quarter qq 1--4 month mm 1--12 day of year dy 1--366

Day dd 1--31 week wk 1--53 weekday dw 1--7 (Sunday -- Saturday)

Hour hh 0--23 minute mi 0--59 second ss 0--59 milisecond MS 0--999 */
-- DATEPART achieves the same effect as DATENAME. DATEPART returns an integer and DATENAME returns a character.

The Code is as follows:
Select DATEPART (qq, getdate ())

-- Get-d from the previous day; + d from the next day

The Code is as follows:
Select getdate ()-1

-- Obtain the yy, mm, dd, and dd of the two time ranges.

The Code is as follows:
Select DATEDIFF (dd, '2017-7-7 ', getdate ())

-- Convert a date of string type to a date type

The Code is as follows:
Select CONVERT (DATETIME, '2017-8-6 '))

-- Add or subtract year, month, and day on the current time

The Code is as follows:
Select DATEADD (mm,-2, getdate ())
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.