Log in  ::  Register  ::  Not logged in

## Fun with Scale and Precision

 Author Message Carlo Romagnano SSChampion Group: General Forum Members Points: 12287 Visits: 3514 Explanation is in the note.Try to reduce the precision.SELECT CAST(cast(1.67574 as decimal(38,10)) / cast(10000 as decimal(38,10)) AS DECIMAL(38,10) ) conv_factor returns 0.0001670000SELECT CAST(cast(1.67574 as decimal(29,10)) / cast(10000 as decimal(29,10)) AS DECIMAL(29,10) ) conv_factor returns 0.0001675740 Steve Eckhart Ten Centuries Group: General Forum Members Points: 1426 Visits: 8664 select cast(1.67574 as decimal(38,10)) = 1.675400000 which has precision 6 and scale 10, right?andselect cast(10000 as decimal(38,10)) = 10000.0000000000 which has precision 1 and scale 10, right?So,p1 - s1 + s2 + max(6, s1 + p2 + 1) = 6 - 10 + 10 + max(6, 10 + 1 + 1) = 6 + 12 = 18.And the result scale is max(6, s1 + p2 + 1) = max(6, 10 + 1 + 1) = 12.So, where is the rounding happening?In Dude76's step by step we see that d1 / d2 is being rounded and is not returning the promised 18 characters of precision and 12 positions of scale. Why? Steve Eckhart sknox SSCertifiable Group: General Forum Members Points: 6888 Visits: 3153 vk-kirov (12/4/2009)...Something about this can be found in BOL, topic "Precision, Scale, and Length (Transact-SQL)": http://technet.microsoft.com/en-us/library/ms190476.aspxWe have two numbers of type NUMERIC(38,10), so their precision = 38 and scale = 10.According to the table from the above link, the result precision is: p1 - s1 + s2 + max(6, s1 + p2 + 1) = 38 - 10 + 10 + max(6, 10 + 38 + 1) = 38 + 49 = 87.The result scale is: max(6, s1 + p2 + 1) = max(6, 10 + 38 + 1) = 49.But there is also a note:* The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.Ok, the result precision (87) is definitely greater than 38, so it was reduced to 38. But why the scale was reduced to 6 – I can't find any explanation Well, if the result precision is 87, and the scale is 49, that's a potential of 49 to the right of the decimal point, leaving (87-49)=38 to the left. Now that's an interesting number. If the note were a hard-and-fast rule (i.e, preserve numbers to the left at all costs), the scale would have to be 0. My guess is that the equations aren't exactly as described in Technet. I expect SQL Server actually does the following process (equivalent to the equations except for cases where resulting precision needs to be reduced to 38):`(p1 = numerator precision, p2 = denominator precision, pR = result precision; equivalent s for scale)-- min() ensures pR is no greater than 38 as mentioned in the note -- in our example, it becomes 38pR = min(p1-s1+s2+max(6,s1+p2+1),38)-- by subtracting from pR and using max(), we ensure a minimum of 6 digits to the right -- in our example this becomes max(38-(38-10+10),6) = max(0,6) = 6!sR = max(pR-(p1-s1+s2),6) `If that's the case, the 6 digits simply comes from their choice of 6 as the minimum scale when precision has to be truncated. sknox SSCertifiable Group: General Forum Members Points: 6888 Visits: 3153 Steve Eckhart (12/4/2009)select cast(1.67574 as decimal(38,10)) = 1.675400000 which has precision 6 and scale 10, right?andselect cast(10000 as decimal(38,10)) = 10000.0000000000 which has precision 1 and scale 10, right?wrong, and wrong. Because you're explicitly casting to decimal(38,10), both of your results have precision of 38 and scale of 10 even though they don't apparently require it. SQL server will not narrow the precision and scale of an explicitly cast decimal result. Colin Frame SSC Eights! Group: General Forum Members Points: 873 Visits: 343 Nadabanan - thanks for spotting this. Have updated with correct text. Forum Etiquette: How to post data/code on a forum to get the best helpMake sure that you include code in the appropriate IFCode tags, e.g. [code=sql][/code]. You can find the IFCode tags under the INSERT options when you are writing a post. Steve Eckhart Ten Centuries Group: General Forum Members Points: 1426 Visits: 8664 As someone stated earlier, the link apparently is incorrect since it doesn't enforce the minimum scale you describe here although we're obviously seeing it. I left feedback on the link that the information on division of decimals is incomplete and referenced this discussion. Steve Eckhart nadabadan Right there with Babe Group: General Forum Members Points: 751 Visits: 1018 vk-kirov (12/4/2009)stewartc-708166 (12/3/2009)the explanation re why the rounding off takes place is.....?Something about this can be found in BOL, topic "Precision, Scale, and Length (Transact-SQL)": http://technet.microsoft.com/en-us/library/ms190476.aspxWe have two numbers of type NUMERIC(38,10), so their precision = 38 and scale = 10.According to the table from the above link, the result precision is: p1 - s1 + s2 + max(6, s1 + p2 + 1) = 38 - 10 + 10 + max(6, 10 + 38 + 1) = 38 + 49 = 87.The result scale is: max(6, s1 + p2 + 1) = max(6, 10 + 38 + 1) = 49.But there is also a note:* The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.Ok, the result precision (87) is definitely greater than 38, so it was reduced to 38. But why the scale was reduced to 6 – I can't find any explanation The ResultingPrecision = 87 and ResultingScale = 49. However, the ResultingPrecision has to be reduced since it is greater than 38. To get to 38, we substract 87 by 49. Since we reduced ResultingPrecision by 49, we need to reduce the ResultingScale by 49 as well. (49 - 49) leaves us a ResultingScale of 0. However max(6, 0) = 6, so the ResultingScale ends up as 6. Therefore, result is truncated ( not rounded ) to 6 decimal digits. So.SELECT cast(1.67574 as decimal(38,10)) / cast(10000 as decimal(38,10))gives us 0.000167 -> precision of 38 and scale of 6soSELECT CAST( cast(1.67574 as decimal(38,10)) / cast(10000 as decimal(38,10)) AS DECIMAL(38,10) ) conv_factorreduces toSELECT CAST( 0.000167 AS DECIMAL(38,10) ) conv_factorwhich gives us 0.0001670000.Took me a while to figure this out. The real question is why SELECT cast(1.67574 as decimal(38,10)) / cast(10000 as decimal(38,10)) gives us 0.000167. The outer CAST is just a silly distraction. tbohm Forum Newbie Group: General Forum Members Points: 1 Visits: 9 So what we're really saying is that SS doesn't divide correctly.sigh Toreador SSCertifiable Group: General Forum Members Points: 5914 Visits: 8277 sknox (12/4/2009)Steve Eckhart (12/4/2009)select cast(1.67574 as decimal(38,10)) = 1.675400000 which has precision 6 and scale 10, right?andselect cast(10000 as decimal(38,10)) = 10000.0000000000 which has precision 1 and scale 10, right?wrong, and wrong. Because you're explicitly casting to decimal(38,10), both of your results have precision of 38 and scale of 10 even though they don't apparently require it. SQL server will not narrow the precision and scale of an explicitly cast decimal result.I'm struggling here.Surely that's exactly what SQLServer is doing?Either way, the explanation attached to the QotD doesn't seem to attempt to explain it - and I'm not surprised! sknox SSCertifiable Group: General Forum Members Points: 6888 Visits: 3153 Toreador (12/8/2009)sknox (12/4/2009)Steve Eckhart (12/4/2009)select cast(1.67574 as decimal(38,10)) = 1.675400000 which has precision 6 and scale 10, right?andselect cast(10000 as decimal(38,10)) = 10000.0000000000 which has precision 1 and scale 10, right?wrong, and wrong. Because you're explicitly casting to decimal(38,10), both of your results have precision of 38 and scale of 10 even though they don't apparently require it. SQL server will not narrow the precision and scale of an explicitly cast decimal result.I'm struggling here.Surely that's exactly what SQLServer is doing?No. The equation in the QotD was:CAST( cast(1.67574 as decimal(38,10)) / cast(10000 as decimal(38,10)) AS DECIMAL(38,10) ) Procedurally, this is what SQL Server does:Step 1 is to cast 1.67574 as decimal(38, 10). This explicit cast is not narrowed, which is one reason why the calculation in (3) results in a result that has to be narrowed.Step 2 is to cast 10000 as decimal(38, 10). This explicit cast is not narrowed, which is the other reason why the calculation in (3) results in a result that has to be narrowed.Step 3 is to perform the division. This results in an answer whose precision and scale are beyond the ranges of the decimal data type. So the precision and scale are narrowed as discussed in this topic. Note that technically, this result is not explicitly cast.Step 4 is to cast the final result (i.e, after the internal narrowing) as decimal(38,10). This explicit cast is also not narrowed, which is why we see the trailing 0s in the result.An explicit cast that falls within the ranges of the decimal data type will not be narrowed. An explicit cast that falls outside the ranges of the decimal data type will result in an error. A calculation which results in precision and scale outside the ranges of the decimal data type will be narrowed to fit within those ranges as discussed.I agree that the explanation on the QotD didn't explain it. That's why I did the research I did.

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.