How to obtain partial time in SQL

Source: Internet
Author: User
Method for obtaining partial time in SQL: Sometimes, we may want to make some data statistics by month, by day, or by year. However, the actual data we save may be a very accurate occurrence time, which may be seconds. How to intercept a part of a time becomes a problem. There are two solutions: the most direct idea to use DatePart or Yea

Method for obtaining partial time in SQL: Sometimes, we may want to make some data statistics by month, by day, or by year. However, the actual data we save may be a very accurate occurrence time, which may be seconds. How to intercept a part of a time becomes a problem. There are two solutions: the most direct idea to use DatePart or Yea

Method for obtaining partial time in SQL:

Sometimes, we may want to make some data statistics on a monthly, daily, or yearly basis. However, the actual data we save may be a very accurate occurrence time, probably in seconds. How to intercept a part of a time becomes a problem.

There are two solutions:

The most direct idea is to use the DatePart or Year, Month, and Day functions.

CAST (

(

STR (YEAR (GETDATE () + '/' +

STR (MONTH (GETDATE () + '/' +

STR (DAY (GETDATE ()))

)

AS DATETIME

)

If you only need one year or one month, the previous one will be saved.

Method 2: Use time computing functions

For example, obtaining a month

DateAdd (month, DateDiff (month, 0, Coltime), 0)

The principle is that the database Time Value Coltime is reduced by 1900-1 to obtain the number of months in this period, and then the system adds-1-1 again.

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.