SQL statement for Time Processing

Source: Internet
Author: User
When doing a project, you often need to process the stored time in the database before performing the operation, so I summarized some

When doing a project, you often need to process the stored time in the database before performing the operation, so I summarized some

The Code is as follows:
-- 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.
/*
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.
Select DATEPART (qq, getdate ())

-- Get-d from the previous day; + d from the next day
Select getdate ()-1

-- Obtain the yy, mm, dd, and dd of the two time ranges.
Select DATEDIFF (dd, '2017-7-7 ', getdate ())

-- Convert a date of string type to a date type
Select CONVERT (DATETIME, '2017-8-6 '))

-- Add or subtract year, month, and day on the current time
Select DATEADD (mm,-2, getdate ())

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.