Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 select decimals after calculations, weird display Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, October 02, 2013 7:05 PM
 Valued Member Group: General Forum Members Last Login: Yesterday @ 10:10 PM Points: 67, Visits: 161
 Hi, I can't get why I have that strange results, I need to keep value of dividing in a column let say 1/35, it's displays as 0 no matter what kind of convert/cast I using,however if I add 35.0 like for a1 then it works.Internally all this results are columns in intermediate temp tables derived as a result of sum() or count(), no any no any special formatting done. c1, c2 alwayse int, but I need very presise result in "a" column. Later I will use "a" in ssrs .`select SUM(1) c1, SUM(35) c2 into #tselect c1, c2, CAST(1/35.0 as decimal(8,6)) a1, CAST(1/35 as decimal(8,6)) a2, CAST(c1/c2 as decimal(8,6)) a3 from #t c1 c2 a1 a2 a31 35 0.028571 0.000000 0.000000`TxMario
Post #1500959
 Posted Wednesday, October 02, 2013 9:09 PM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 11:48 PM Points: 3,149, Visits: 4,412
 CAST before you divide:`SELECT CAST(c1 AS DECIMAL(8,6)) / CAST(c2 AS DECIMAL(8,6))FROM #t;`Or not:`SELECT 1.*c1 / c2FROM #t;` No loops! No CURSORs! No RBAR! Hoo-uh!INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!Skype me if you can: dwaincamps
Post #1500971
 Posted Thursday, October 03, 2013 1:10 AM
 SSChampion Group: General Forum Members Last Login: Yesterday @ 12:50 PM Points: 11,190, Visits: 8,035
 To give a little more information about dwain's answer (he is totally correct):you are doing an integer division before you cast it to a decimal.This means the result is rounded to zero before you cast. How to post forum questions.Need an answer? No, you need a question.What’s the deal with Excel & SSIS?Member of LinkedIn. My blog at LessThanDot. MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1501018
 Posted Thursday, October 03, 2013 7:56 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 4:43 AM Points: 34,537, Visits: 28,702
 dwain.c (10/2/2013)CAST before you divide:`SELECT CAST(c1 AS DECIMAL(8,6)) / CAST(c2 AS DECIMAL(8,6))FROM #t;`Or not:`SELECT 1.*c1 / c2FROM #t;`You can also cheat if you're not too worried about precision (more on that in a minute)...`SELECT (c1+0.0)/ c2 FROM #t;`If you are worried about precision (and I normally am) during Division or Multiplication of Decimal numbers, like when you're calculating periodic interest and the "whole" must come out to the correct penny, then Dwain's method will work the best except that you must increase the both the scale and the precision of the conversions by CAST. Do your calculations with a large precision (I recommend at least 15 decimal places like what a calculator does) and then do a final cast for display purposes if the display requires less precision. --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1501195
 Posted Thursday, October 03, 2013 9:40 AM
 Valued Member Group: General Forum Members Last Login: Yesterday @ 10:10 PM Points: 67, Visits: 161
 Thanks, allThansk, DwaineM
Post #1501257

 Permissions