简体   繁体   English

从十进制数中删除尾随零

[英]Remove trailing zero from decimal number

I have a one database table field called Amount which type is decimal(18,6). 我有一个名为Amount的数据库表字段,其类型为十进制(18,6)。 so it is stored in database up to 6 decimal points like 9.786534 But while retrieving that field using select query i have to take care like following 所以它存储在数据库中,最多6个小数点,如9.786534但是当使用select查询检索该字段时,我必须小心,如下

  1. Remove trialling zero eg if number is 9.230000 then result is only 9.23 删除trialling zero,例如,如果number为9.230000,则结果仅为9.23
  2. If decimal points are all zero then only remove only four trialling zero eg If number is 9.000000 then result is 9.00 如果小数点全为零,则仅删除四个试验零,例如,如果数字为9.000000,则结果为9.00

  3. Result is up to 2 decimal point if there are trialling zero. 如果有零件,则结果最多为2位小数。

If we write simple query like 如果我们写简单的查询就像

select TOP 1 Amount From EmployeeMaster 从EmployeeMaster中选择TOP 1金额

then it gives 9.230000 然后它给出了9.230000

but my intension is to remove trailing zero.. 但我的意图是删除尾随零..

Please help me.. 请帮我..

It works for removing trailing zeros, but I am still not able to convert 9 to 9.00 in this method. 它适用于删除尾随零,但我仍然无法在此方法中将9转换为9.00

Declare @myvalue varchar(50), 
        @Price Varchar(50) 

Set @Price = '9.230000' 

set @Myvalue = reverse(substring(@Price,patindex('%.%',@Price)+1,len(@Price))) 


SELECT 
case  
When  patindex('%.%[1-9]%',@price) = 0 Then  
    substring(@price,1,patindex('%.%',@price)-1) 
else 
    substring(@price,1,patindex('%.%',@price)-1) + '.' +  Reverse(substring(@Myvalue,patindex('%[1-9]%',@Myvalue),len(@Myvalue))) 
END 

Coming from decimal(18,6) you could do... 来自小数(18,6),你可以做...

select cast(Amount as decimal(18,2))

Most databases that support the CAST function will round the number while converting it. 大多数支持CAST功能的数据库会在转换数据时对其进行舍入。 On SQLServer this is what I would do if I wanted rounding. 在SQLServer上,如果我想要舍入,我会这样做。

If what you actually want is a string with only two digits after the decimal then you could 如果您真正想要的是一个只有小数点后两位数的字符串,那么您可以

select cast((Amount as decimal(18,2)) as nvarchar)

nvarchar is SQLServer's variable length unicode type. nvarchar是SQLServer的可变长度unicode类型。 Databases do not agree much on string types. 数据库对字符串类型不太一致。 Your database may have a different one. 您的数据库可能有不同的数据库。 The rest of that sql is ANSI standard. 该sql的其余部分是ANSI标准。 Not all dbs support that either but many do. 并非所有dbs都支持这一点,但很多人都支持。

这应该工作

SELECT CAST(REPLACE(RTRIM(REPLACE(CAST(CAST(33.9082976 AS DECIMAL(38,8)) AS NVARCHAR(256)),'0',' ')),' ','0') AS FLOAT)

这有用吗?

select TOP 1 ROUND(Amount, 2) From EmployeeMaster 

TRY below mentioned code. 尝试下面提到的代码。

SELECT TOP 1 CONVERT(DECIMAL(10,2),Amount) From EmployeeMaster 

Hope it will work as expected. 希望它能按预期工作。

An alternative approach: 另一种方法:

1) convert the decimal to a string; 1)将小数转换为字符串;

2) split the string into 2 parts, separating the last 4 characters from the rest of the string; 2)将字符串分成2部分,将最后4个字符与字符串的其余部分分开;

3) remove trailing zeros from the last 4 characters; 3)从最后4个字符中删除尾随零;

4) concatenate the two parts back. 4)将两部分连接起来。

WITH data (V)     AS (SELECT CAST(9.786534 AS decimal(18,6))
                      UNION ALL
                      SELECT CAST(9.78653  AS decimal(18,6))
                      UNION ALL
                      SELECT CAST(9.7800   AS decimal(18,6))
                      UNION ALL
                      SELECT CAST(9.7      AS decimal(18,6))
                      UNION ALL
                      SELECT CAST(9.00000  AS decimal(18,6))
                     )

,    AsString (V) AS (SELECT CAST(V AS varchar) FROM data)

,    Split (L, R) AS (SELECT LEFT(V, LEN(V) - 4), RIGHT(V, 4) FROM AsString)

,    Adjusted     AS (SELECT L,
                             REPLACE(RTRIM(REPLACE(R, '0', ' ')), ' ', '0') AS R
                      FROM Split)

SELECT Result = L + R FROM Adjusted

The output of the above script is: 上述脚本的输出是:

Result
--------
9.786534
9.78653
9.78
9.70
9.00

I guess using patindex in your case: 我想在你的情况下使用patindex:

CASE WHEN FLOOR(Amount) <> CEILING(Amount) THEN 
     LTRIM(SUBSTRING(STR(Amount, 18, 6), 1, LEN(STR(Amount, 18, 6)) - PATINDEX('%[^0]%', REVERSE(str(Amount, 18, 6))) + 1))
     ELSE STR(Amount,18,2)
END

for a decimal(18,6) field this should work: 对于decimal(18,6)字段,这应该工作:

select trim(to_char(Amount, '999999999999999999.99')) from EmployeeMaster 

(at least for Oracle, not sure about other types) (至少对于Oracle,不确定其他类型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM