--=============================================--Author: <author,,cc>--Create Date: <create date, the- to- - Ten: +:07.080>--Description: <description, the decimal point is converted to a percentage, and the 3rd bit is carried 4 5 into the >--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 @iint=3Declare @itint=7Select(@i*1.0)/@it [decimal point]--Because the data type is int, you need *1.% to convert, if it is directly with the number of decimal places can skip this stepSelect((@i*1.0)/@it) * -[Molecular]--To convert the decimal point to the desired percentage, the molecular value needs to be multiplied bySelectCAST (((@i*1.0)/@it) * - as decimal(5,0) ) [Percentage of 2 decimal places after 4-5 entry]--Keep 2 decimal places, and also a step to 4 5 in decimal placesSelectLTRIM (CAST ((@i*1.0)/@it) * - as decimal(5,0)))+'%'[Percentage]--Plus%number as a synthetic percentage, or you can use the following method to convert because another method can only use the Convert varchar typeSelectCast (((@i*1.0)/@it) * - as decimal(5,0)) asvarcharTen))+'%'[Percentage]-------------------------Method 2-----------------------------------------DECLARE @iint=3Declare @itint=7Select(@i*1.0)/@it [decimal point]--Because the data type is int, you need *1.% to convert, if it is directly with the number of decimal places can skip this stepSelect((@i*1.0)/@it) +0.005[4 5 into]--To get the results of +0. 005 to get the third decimal point 445 entry resultSelect(((@i*1.0)/@it) +0.005)* -[numerator with decimal point]-- * -A molecule that converts a decimal point to a percentageSelectCAST ((((@i*1.0)/@it) +0.005)* - as int) [Molecular]--The 2nd digit after the decimal point is intercepted, converted to an integerSelectCast ((((@i*1.0)/@it) +0.005)* - as int) asvarcharTen)) +'%'[Percentage]--Converting an integral type to a character data type, plus a percent sign
T-SQL decimal point conversion percentage
T-SQL decimal point conversion percentage