SQL Server removes the extra 0 after the decimal point

Source: Internet
Author: User

What do I do if I want to remove the extra 0 from the decimal point in SQL?

Select 5000/10000.0--Want to become 0.5
Select 5500/10000.0--Want to become 0.55
Select 5550/10000.0--Want to become 0.555
Select 5555/10000.0--Want to become 0.5555

The results were: 0.5000000 0.5500000 0.5550000 0.5555000

First, if you want to remove the number 5 after the excess 0, need to convert:

Select CONVERT (float,5000/10000.0)--want to become 0.5
Select CONVERT (float,5500/10000.0)--want to become 0.55
Select CONVERT (float,5550/10000.0)--want to become 0.555
Select CONVERT (float,5555/10000.0)--want to become 0.5555

The results were: 0.5 0.55 0.555 0.5555

Second, create the function:

In SQL Server, create a function Clearzero, use this function to remove the extra 0 after the decimal point.

CREATE function [dbo]. [Clearzero] (@inValue varchar (50))
Returns varchar (50)
As
Begin
DECLARE @returnValue varchar (20)
if (@inValue = ")
Set @returnValue = "--Empty when empty
else if (charindex ('. ', @inValue) = ' 0 ')
Set @[email protected]--for non-decimal
else if (substring (reverse (@inValue), Patindex ('%[^0]% ', reverse (@inValue)), 1) = '. ')
Set @returnValue =left (@inValue, Len (@inValue)-patindex ('%[^0]% ', reverse (@inValue)))--for all 0 after the decimal point
Else
Set @returnValue =left (@inValue, Len (@inValue)-patindex ('%[^0]%.% ', reverse (@inValue)) +1)--any other situation
Return @returnValue
End

Another: In C #?

Decimal d = 0.0500m;

D.tostring ("0.##") came out.

You can also do this string. Format ("{0:0.##}", d000)

SQL Server removes the extra 0 after the decimal point

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.