How do I get only the numbers after the decimal?
Example: 2.938
= 938
The usual hack (which varies a bit in syntax) is
x - floor(x)
That's the fractional part. To make into an integer, scale it.
(x - floor(x)) * 1000
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
If you want to select only decimal numbers use this WHERE clause:
(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
You can use 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
:
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
one way, works also for negative values
declare @1 decimal(4,3)
select @1 = 2.938
select PARSENAME(@1,1)
try this:
SELECT (num % 1)