Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
i will try to after 4 decimal point?
DECLARE @tbl TABLE (val float)
INSERT INTO @tbl SELECT 12.19
INSERT INTO @tbl SELECT 13.92
INSERT INTO @tbl SELECT 14.47
INSERT INTO @tbl SELECT 18.025
INSERT INTO @tbl SELECT 18.3
INSERT INTO @tbl SELECT 17.79
INSERT INTO @tbl SELECT 18.13
INSERT INTO @tbl SELECT 15.245
INSERT INTO @tbl SELECT 12.485
INSERT INTO @tbl SELECT 11.365
INSERT INTO @tbl SELECT 18.26
INSERT INTO @tbl SELECT 15.44
INSERT INTO @tbl SELECT 19.13
INSERT INTO @tbl SELECT 15.59
INSERT INTO @tbl SELECT 28.695
INSERT INTO @tbl SELECT 29.35
INSERT INTO @tbl SELECT 15.27
INSERT INTO @tbl SELECT 15.895
INSERT INTO @tbl SELECT 26.51


SELECT *,((abs(val)*10000) - CONVERT(INT,(abs(val)*10000))),cast(val*power(10,4) as int),val*power(10,4)
from @tbl


What I have tried:

But only one number most of confusion

SQL

SELECT ((abs(cast(18.26 as float))*10000) - CONVERT(INT,(abs(cast(18.26 as float))*10000)))
Posted
Updated 25-Oct-18 23:45pm
Comments
OriginalGriff 25-Oct-18 3:47am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what number you want to change, or what you are trying to change it to ...
Use the "Improve question" widget to edit your question and provide better information.
jsc42 25-Oct-18 3:52am    
I am not sure what you are trying to achieve. Are you trying to truncate numbers at 4 dec places or to display them with 4 dec places. Please tell us the expected output and the actual output.

I have tried your code in SQLServer and your 2nd col produces zeros for all values except for 18.26 which gives 2.91038304567337E-11 which is almost zero - this will be a rounding error. Float will always give inexact values as the binary value is only an approximation of the decimal value which is something you must be aware of when working with float values (in any language, not just SQL). All of the values will have rounding errors but some will be too small to display so they are shown as 0.

1 solution

You can try CAST

SQL
SELECT *,cast(((abs(val)*10000) - CONVERT(INT,(abs(val)*10000))) as Float), cast(val*power(10,4) as int),val*power(10,4)
from @tbl
 
Share this answer
 
Comments
kskumaran 30-Oct-18 2:32am    
SELECT *,ISNULL(ROUND((abs(val)*10000) - CONVERT(INT,(abs(val)*10000)),0),0), cast(val*power(10,4) as int),val*power(10,4)
from @tbl
kskumaran 30-Oct-18 2:33am    
above this answer is correct

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900