Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only decimal part in float number in SQL Server [closed]

I have following number in SQL Server 2008 : 5.4564556
I just want to get only 4564556 from 5.4564556
it would be better if i can get only 4 among 4564556 that is first digit of fractional part..

How can i do it?

Thanks

like image 743
ghanshyam.mirani Avatar asked Aug 26 '12 10:08

ghanshyam.mirani


People also ask

How do I get only the decimal part of a number in SQL?

Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).

How get after decimal value in SQL Server?

For bigint use SELECT substr((2.938 % 1)::text,3)::bigint; , else use text result.

How can I get integer part of a number in SQL?

The Int() function returns the integer part of a number.

How check value is decimal or not in SQL Server?

SQL Server ISNUMERIC() Function The ISNUMERIC() function tests whether an expression is numeric. This function returns 1 if the expression is numeric, otherwise it returns 0.


2 Answers

You can try in this way :

SELECT (5.4564556 % 1)
like image 108
aleroot Avatar answered Nov 06 '22 19:11

aleroot


You can also use FLOOR():

SELECT Value - FLOOR(Value)

In your case:

5.4564556 - 5 = 0.4564556

in case of negative value you can use ABS

SELECT ABS(Value) - ABS(FLOOR(Value))
like image 31
Dor Cohen Avatar answered Nov 06 '22 20:11

Dor Cohen