Cast (), Decimal (m,d)--sql Preserve decimal operations on query fields

Source: Internet
Author: User

Reference: http://database.51cto.com/art/201005/201651.htm

Http://www.lai18.com/content/1693593.html

Directly on the example, the following is the processing of the field cost divided by 1000, leaving two decimal places

Cast (SUM (a.costs)/1000 as Decimal (32,2))

The cast () function is a conversion function, which is an expression that includes the source value and the target data type separated by the AS keyword.

1) Convert the string ' 123 ' to int integer type:

Select CAST (' 123 ' as int)    #out: 123

2) Neither the CAST () function nor the CONVERT () function can perform rounding or truncation operations:

SELECT CAST (' 123.4 ' as int)

Since 123.4 cannot be represented by an int data type, a call to this function will result in an error.

decimal (m,d) is a data type with the parameter M as the numeric precision, the precision being the total number of digits (the sum of the decimal and integer digits), and D being the number of decimal digits to keep.

Note: 1) parameter m<65 is the total number, d<30 and d<m are decimal digits.

2) The maximum possible range of decimal values is the same as a double, but the valid range of values is determined by the values of M and D. If M is changed and the D is fixed, the range of values will be larger with M.

    

Cast (), Decimal (m,d)--sql Preserve decimal operations on query fields

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.