-- =============================================--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