T-SQL decimal point conversion percentage

Source: Internet
Author: User

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

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.