T-SQL decimal point conversion percentage

Source: Internet
Author: User

-- =============================================--Author: <Author,,CC>--Create Date: <create date, 2014-05-26 10:43:07.080>--Description: <description, the decimal point is converted to a percentage, and the 3rd digit is 4 5 in >--Environment: <version,, Microsoft SQL Server R2 (RTM)-10.50.1600.1 (X64)) >-- =============================================/*------------------------------------------------------------------------2 Ways of thinking: 1 Kinds of direct decimal type can be set to a precision of 4, The decimal place is 2, which automatically rounds out the number of decimals to 3 and 2 is the number of decimal places that we add to meet the rounding criteria (that is, the variable + 0.005) This time you can use the int type below as the implementation method:--------------------------------------------------------------------------*/                -------------------------Method 1---------------------------------------Declare @i int = 3Declare @it int = 7Select(@i*1.0)/@it [decimal point] --because the data type is int, you need to convert the * 1.0来, if it is directly with the number of decimal places you can skip this stepSelect((@i*1.0)/@it)* - [molecule]--To convert the decimal point to the desired percentage, the molecular value needs to be multiplied bySelect CAST( ((@i*1.0)/@it)* -  as decimal(5,0))[Percentage of 2 decimal places after 4 5 in]    --Keep 2 decimal places, and also a step to 4 5 in decimal placesSelect LTRIM(CAST( ((@i*1.0)/@it)* -  as decimal(5,0)))+'%'     [percentage]--add% to the composition percentage, or you can use the following method to convert because another method can only use the Convert varchar typeSelect CAST(CAST( ((@i*1.0)/@it)* -  as decimal(5,0)) as varchar(Ten))+ '%'  [percentage]-------------------------Method 2-----------------------------------------Declare @i int = 3 Declare @it int = 7Select(@i*1.0)/@it    [decimal point]--because the data type is int, you need to convert the * 1.0来, if it is directly with the number of decimal places you can skip this stepSelect((@i*1.0)/@it)+0.005 [4 5 in]--+0.005 of the resulting results to obtain the third decimal point 445 entry ResultsSelect(((@i*1.0)/@it)+0.005)* - [a molecule with a decimal point]--*100 A molecule that converts a decimal point to a percentageSelect CAST((((@i*1.0)/@it)+0.005)* -  as int)[molecule]--The 2nd digit after the decimal point is intercepted, converted to an integerSelect CAST(CAST((((@i*1.0)/@it)+0.005)* -  as int) as varchar(Ten))+ '%' [percentage]--converts an integral type to a character data type, plus a percent sign

The above method is just an example of a percentage, and may actually need to be fine-tuned based on data type and requirements

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.