Log in  ::  Register  ::  Not logged in

## ROUND() function doesn't round properly

 Author Message ESB Forum Newbie Group: General Forum Members Points: 4 Visits: 17 Hello. I use SQL Server 2005 and I'm calculating the age of patients in the following manner:`DATEDIFF(DAY, birthdate, operationdate)/365.25`The output always seems to be a decimal with 6 decimal digits. The rounding problem arises with border cases like for instance an age of 60.444445. If you round this properly it should result in 60.45 and that should result in 60.5. However, when I use ROUND(60.444445, 1) it results in 60.4!The problem is that SQL Server seems to ignore the digits after the rounding border. ROUND(60.449999, 1) for instance also results in 60.4. It seems to truncate it 1 position behind the rounding index and starts rounding after that. I found out that using the ROUND() function repeatedly in the following manner solves the issue but it is a bit ridiculous:`PRINT ROUND(ROUND(ROUND(ROUND(ROUND(60.444445, 5), 4), 3), 2), 1)`Is there any other way to solve this and am I missing something?Thanks in advance! Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 Heres how to calculate an age`select CASEWHEN dateadd(year, datediff (year, d1, d2), d1) > d2THEN datediff (year, d1, d2) - 1ELSE datediff (year, d1, d2)END as Age`I'll take a look at the rounding issue though... Clear Sky SQLMy Blog Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 I think you are wrong in your assessment of round though,round(60.444445,1) = 60.4 This `PRINT ROUND(ROUND(ROUND(ROUND(ROUND(60.444445, 5), 4), 3), 2), 1)`works because you are rounding a rounded , rounded, rounded , rounded number (ie the 5 has for want of a better word been cascaded up) Clear Sky SQLMy Blog ESB Forum Newbie Group: General Forum Members Points: 4 Visits: 17 For some reason that way of rounding was in my head. But I spoke with others about and I think SQL Server does round it correctly yes. Jeff Moden SSC-Forever Group: General Forum Members Points: 45000 Visits: 39880 Dave Ballantyne (10/2/2009)Heres how to calculate an age`select CASEWHEN dateadd(year, datediff (year, d1, d2), d1) > d2THEN datediff (year, d1, d2) - 1ELSE datediff (year, d1, d2)END as Age`I'll take a look at the rounding issue though...That's one of the simplest I've seen. Correctly handles the Feb 29 problem that most other's don't handle, too! --Jeff ModenRBAR 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.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 Cant take the credit for it ...http://windowsitpro.com/article/articleid/14267/how-can-i-calculate-someones-age-in-sql-server.html Clear Sky SQLMy Blog Jeff Moden SSC-Forever Group: General Forum Members Points: 45000 Visits: 39880 Shoot... the link is broken. Thanks anyway, Dave. --Jeff ModenRBAR 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.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 Seems fine now, just a temporary thing i guess.TBH , its hardly worth the click though :-) simple Q and A Clear Sky SQLMy Blog Jeff Moden SSC-Forever Group: General Forum Members Points: 45000 Visits: 39880 Hmmmm.... something must be wrong on my end. It keeps giving me a 404 error.Q & A's are sometimes pretty rich in information like the little date jewel you found. --Jeff ModenRBAR 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.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs hrothenb Forum Newbie Group: General Forum Members Points: 7 Visits: 64 DECLARE @Test TABLE ( EmpName VARCHAR(40) , BirthDate DATE );INSERT INTO @Test ( EmpName , BirthDate )VALUES ( '30 Yrs old yesterday' , DATEADD(day , -1 , DATEADD(year , -30 , GETDATE())) ) , ( '30 Yrs old today' , DATEADD(year , -30 , GETDATE()) ) , ( '30 Yrs old tomorrow' , DATEADD(day , 1 , DATEADD(year , -30 , GETDATE())) );SELECT EmpName , BirthDate , CASE WHEN DATEADD(YY , DATEDIFF(yy , BirthDate , GETDATE()) , BirthDate) < GETDATE() THEN DATEDIFF(yy , BirthDate , GETDATE()) ELSE DATEDIFF(yy , BirthDate , GETDATE()) - 1 END AS AgeFROM @Test;

## 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 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 14 - Administration      SQL Server 14 - 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 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         Red Gate Software         Quest Software         ApexSQL         Lumigent         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-2017 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.