SQL - 如何只获取小数点后的数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3418606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL - How do I get only the numbers after the decimal?
提问by user380432
How do I get only the numbers after the decimal?
如何只得到小数点后的数字?
Example: 2.938
= 938
例子:2.938
=938
回答by Pavel Morshenyuk
try this:
尝试这个:
SELECT (num % 1)
回答by SQLMenace
one way, works also for negative values
一种方式,也适用于负值
declare @1 decimal(4,3)
select @1 = 2.938
select PARSENAME(@1,1)
回答by RedFilter
You can use FLOOR
:
您可以使用FLOOR
:
select x, ABS(x) - FLOOR(ABS(x))
from (
select 2.938 as x
) a
Output:
输出:
x
-------- ----------
2.938 0.938
Or you can use SUBSTRING
:
或者你可以使用SUBSTRING
:
select x, SUBSTRING(cast(x as varchar(max)), charindex(cast(x as varchar(max)), '.') + 3, len(cast(x as varchar(max))))
from (
select 2.938 as x
) a
回答by Tejasvi Hegde
More generalized approach may be to merge PARSENAME and % operator. (as answered in two of the answers above)
更通用的方法可能是合并 PARSENAME 和 % 运算符。(如以上两个答案中的回答)
Results as per 1st approach above by SQLMenace
SQLMenace 根据上述第一种方法得出的结果
select PARSENAME(0.001,1)
Result: 001
结果:001
select PARSENAME(0.0010,1)
Result: 0010
结果:0010
select PARSENAME(-0.001,1)
Result: 001
结果:001
select PARSENAME(-1,1)
Result: -1 --> Should not return integer part
结果:-1 --> 不应返回整数部分
select PARSENAME(0,1)
Result: 0
结果:0
select PARSENAME(1,1)
Result: 1 --> Should not return integer part
结果:1 --> 不应返回整数部分
select PARSENAME(100.00,1)
Result: 00
结果:00
Results as per 1st approach above by Pavel Morshenyuk "0." is part of result in this case.
根据 Pavel Morshenyuk "0" 的上述第一种方法得出的结果。在这种情况下是结果的一部分。
SELECT (100.0001 % 1)
Result: 0.0001
结果:0.0001
SELECT (100.0010 % 1)
Result: 0.0010
结果:0.0010
SELECT (0.0001 % 1)
Result: 0.0001
结果:0.0001
SELECT (0001 % 1)
Result: 0
结果:0
SELECT (1 % 1)
Result: 0
结果:0
SELECT (100 % 1)
Result: 0
结果:0
Combining both:
两者结合:
SELECT PARSENAME((100.0001 % 1),1)
Result: 0001
结果:0001
SELECT PARSENAME((100.0010 % 1),1)
Result: 0010
结果:0010
SELECT PARSENAME((0.0001 % 1),1)
Result: 0001
结果:0001
SELECT PARSENAME((0001 % 1),1)
Result: 0
结果:0
SELECT PARSENAME((1 % 1),1)
Result: 0
结果:0
SELECT PARSENAME((100 % 1),1)
Result: 0
结果:0
But still one issue which remains is the zero after the non zero numbers are part of the result (Example: 0.0010 -> 0010). May be one have to apply some other logic to remove that.
但仍然存在的一个问题是非零数字是结果的一部分之后的零(例如:0.0010 -> 0010)。可能必须应用一些其他逻辑来删除它。
回答by S.Lott
The usual hack (which varies a bit in syntax) is
通常的 hack(在语法上略有不同)是
x - floor(x)
That's the fractional part. To make into an integer, scale it.
那是小数部分。要变成整数,请对其进行缩放。
(x - floor(x)) * 1000
回答by Renato
I had the same problem and solved with '%' operator:
我遇到了同样的问题并用 '%' 运算符解决了:
select 12.54 % 1;
回答by user2412524
Make it very simple by query:
通过查询使其变得非常简单:
select substr('123.123',instr('123.123','.')+1, length('123.123')) from dual;
Put your number or column name instead 123.122
输入您的编号或列名 123.122
回答by Seamus Campbell
If you know that you want the values to the thousandths, place, it's
如果你知道你想要千分之一的值,地方,它是
SELECT (num - FLOOR(num)) * 1000 FROM table...;
回答by Jonas Fermefors
If you want to select only decimal numbers use this WHERE clause:
如果您只想选择十进制数,请使用此 WHERE 子句:
(CAST(RIGHT(Myfield, LEN( Myfield)-CHARINDEX('.',Myfield)+1 ) AS FLOAT)) <> 0
If you want a clear list you can sort by decimal/integer:
如果你想要一个清晰的列表,你可以按十进制/整数排序:
CASE WHEN 0 = CAST(RIGHT(Myfield, LEN( Myfield)-CHARINDEX('.',Myfield)+1 ) AS FLOAT) THEN 'Integer' ELSE 'Decimal' END AS Type
回答by MrPmosh
X - TRUNC(X), works for negatives too.
X - TRUNC(X),也适用于底片。
It would give you the decimal part of the number, as a double, not an integer.
它会给你数字的小数部分,作为双精度数,而不是整数。