SQL Tips GROUP by datetime Type field, take only the date part

Source: Internet
Author: User

Often encountered in the work, in order to query the SQL report, query results are required by date to list, or by day, or by month, year.

This time we are often distressed, datetime is accurate to milliseconds, if the simple group by datetime will result in incorrect results.

This is what we can do with the CONVERT function:

SELECT CONVERT (varchar), createdate,120, var1, Var2from tablegroup by convert (varchar), createdate,120) ASC

The first parameter is the type of conversion, 10 is the length, and the need to eradicate it is adjusted.

The second parameter is a datetime-type field.

The third parameter is a date format. (Detailed format parameters can be consulted: http://www.w3school.com.cn/sql/func_convert.asp)

SQL Tips GROUP by datetime Type field, take only the date part

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.