Calculation results in SQL Server retain decimals

Source: Internet
Author: User

Always keep decimals, calculation in the program is too cumbersome, but also to convert the operation. It is convenient to operate directly in the database.
The data type is defined as Decimal/numeric type, the decimal place needs to be arbitrarily set, divisor and dividend must have a type in decimal/numeric,
The following example:
SELECT 24.0000/38.0000--0
SELECT CAST (24/38 as NUMERIC (18,4))--0
SELECT CAST (24/38.0000 as NUMERIC (18,4))--0.6316 This result has been rounded
SELECT 24.0000/38.0000--0.6316

Decimal/numeric and int type operations result in the Decimal/numeric type because the decimal/numeric has a higher precedence than int.

Refer to the Type Priority table on MSDN:

SQL Server 2005 uses the following order of precedence for data types:

    1. User-defined data type (highest)

    2. sql_variant

    3. XML

    4. datetime

    5. smalldatetime

    6. float

    7. Real

    8. decimal

    9. Money

    10. smallmoney

    11. bigint

    12. int

    13. smallint

    14. tinyint

    15. bit

    16. ntext

    17. text

    18. Image

    19. timestamp

    20. uniqueidentifier

    21. nvarchar(including nvarchar (max))

    22. nchar

    23. varchar (including varchar (max))

    24. Char

    25. varbinary(including varbinary (max))

    26. binary(lowest)

Calculation results in SQL Server retain decimals

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.