如何从具有“最活跃”小数点的列中获取金钱记录。例如,从列表(0.01
,0.0112
和0.001
)中,结果应为0.0112
。
我的第一个想法是将值转换为varchar
并获得最长的记录。但这没有用,因为如果我不想将其取整,则必须指定长度。这是脚本:
SELECT CONVERT(VARCHAR, CAST(Value as DECIMAL(18, 6))) FROM Table
答案 0 :(得分:1)
也许是这样:
DECLARE @tbl TABLE(ID INT IDENTITY, YourValue MONEY, Explanation VARCHAR(150));
INSERT INTO @tbl VALUES
(0.0,'just zero')
,(0.1,'1 decimal in 0.1')
,(1.1,'1 decimal in 1.1')
,(-1.1,'1 decimal in -1.1')
,(123456789.1,'1 decimal in 123456789.1')
,(1.1234,'4 decimals in 1.1234')
,(1.12345,'4 decimals in 1.12345'); --5 is rounded implicitly
-您不需要所有的列,我只是将它们放在可视化的中间步骤中
SELECT *
,YourValue % 1 AS TrickWithModulo
,ABS(YourValue)-FLOOR(ABS(YourValue)) AS MathSolution
,CAST(ABS(YourValue) % 1 AS FLOAT) AS ConvertedToFloat_no_trailin_zeros
,SUBSTRING(CONVERT(VARCHAR(100),CAST(ABS(YourValue) % 1 AS FLOAT)),3,100) JustTheDecimals
,LEN(SUBSTRING(CONVERT(VARCHAR(100),CAST(ABS(YourValue) % 1 AS FLOAT)),3,100)) Length_of_JustTheDecimals
FROM @tbl
ORDER BY Length_of_JustTheDecimals DESC;
这个想法是
0.