I need to extact fractional part of double in sql, but without "0." For example: number: 5.67 int: 5 fact: 67
Asked
Active
Viewed 85 times
2
-
How many digits? All of them? There can be plenty. A certain number? Then how to round? This is also quite a strange requirement (and as such, can be poorly supported depending on specifics) -- what's the intended use? – ivan_pozdeev Apr 21 '19 at 16:48
-
If this is for money, [you need to use fixed rather than floating point](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency/3730040#3730040). – ivan_pozdeev Apr 21 '19 at 16:53
-
@Женя Касьян my answers below have fiddle corrections .Have a check on it!!!! – nikhil sugandh Oct 28 '20 at 23:22
3 Answers
1
In most databases, you can use something like this:
select cast((x - floor(x)) * 100 as int)
In SQL Server, you have other options. For instance, to get the fractional part, you can use %:
select (x % 1)
To get two digits:
select floor(100 * (x % 1)) -- or perhaps round()
or:
select (x * 100) % 100
Gordon Linoff
- 1,198,228
- 53
- 572
- 709
0
SELECT 5.67 AS Doubles, CAST(5.67 AS INT) AS Fractional_Only,
PARSENAME(5.67,1) AS DECIMAL_ONLY
Devesh Kumar Singh
- 19,767
- 5
- 18
- 37
satendra rawat
- 1
- 1
-1
this will work in oracle:
select substr(to_char(5.67),instr(to_char(5.67),'.')+1) from dual;
check http://sqlfiddle.com/#!4/e2ac5e/1074
for mssql :
select substring(CONVERT(varchar, 25.65),CHARINDEX('.', CONVERT(varchar, 25.65))+1,5);
http://sqlfiddle.com/#!18/9eecb/41866
check the last one :
select substring(CONVERT(varchar, (15+15)/2.0),
CHARINDEX('.', CONVERT(varchar, (15+15)/2.0))+1,1);
nikhil sugandh
- 3,422
- 4
- 16
- 29
-
-
-
@ЖеняКасьян take the column + two times or * 2 and divide it by 2.0 to convert it to double – nikhil sugandh Apr 21 '19 at 16:36