## Rounding a number up to the nearest 5

 Author Message smrobin Mr or Mrs. 500 Group: General Forum Members Points: 580 Visits: 136 So I am attempting to mimic some legacy code on an old server at my work. It has a calculation to determine cost. What I need to do is round the number up to the nearest 5th.For example:1.0 -> 5 (everything less than 5 gets rounded to give)5-> 55.75->58->1011->1586.5->90so any number that has a 5 in it before the decimal - will stay rounded to 5. Example - 75.65 -> 75. I have the following code:declare @mon numeric(10,2)set @mon = 86.5 select case when (round(@mon/5,0)*5) < 5 then 5 else (round(@mon/5,0)*5) endHowever - when I run it - 86.5 gets rounded to 85. I need it to round to 90.Any suggestions? Sean Lange SSC Guru Group: General Forum Members Points: 149945 Visits: 18581 You can use a tally for this pretty easily.Here is the code to create a tally table view. This is super fast!!!`create View [dbo].[Tally] asWITH E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)), E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max cteTally(N) AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4 )select N from cteTallyGO`Now we just need to create your query. Please note that I used a cte here because we didn't have a table of data to work with.`with MyData (MyValue, Target) as( select 1.0, 5 union all select 5, 5 union all select 5.75, 5 union all select 8, 10 union all select 11, 15 union all select 86.5, 90)select *from MyData dcross apply ( select top 1 * from Tally where N >= d.MyValue and N % 5 = 0 order by N) x`--EDIT--You can read more about tally tables here. http://www.sqlservercentral.com/articles/62867/ _______________________________________________________________Need help? Help us help you. Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.Need to split a string? Try Jeff Modens splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2) LinksUp Hall of Fame Group: General Forum Members Points: 3165 Visits: 4946 Though not as cool a solution as Mr. Lange's tally table, this alternative is more math oriented than set oriented and is very fast. Plus you could easily turn this into a SP passing any value that you want to round to. NOTE: I borrowed Mr. Lange's cte to demo the solution. (Thank-you!)`;with MyData (MyValue, Target) as( select 0.5, 5 union all select 1.0, 5 union all select 2.0, 5 union all select 5, 5 union all select 5.75, 5 union all select 8, 10 union all select 11, 15 union all select 50, 50 union all select 50.5, 50 union all select 50.75, 50 union all select 51, 55 union all select 54.99, 55 union all select 86.5, 90) Select d.MyValue, d.Target, case when d.MyValue % 5 < 1 then FLOOR(d.MyValue) when d.MyValue % 5 < 3 then FLOOR ((d.MyValue + (5.0 / 2.0)) / 5) * 5.0 + 5.0 else FLOOR((d.MyValue + (5.0 / 2.0)) / 5) * 5.0 end as Result from MyData d`The case statement was required because you are rounding UP to the nearest 5, not just to the nearest 5. __________________________________________________________________________________________________________How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/ smrobin Mr or Mrs. 500 Group: General Forum Members Points: 580 Visits: 136 Thanks for the reply - the problem I have is that I don't always know what the number will be. I have the rules to which I apply to the numbers.I was hoping to write a simple case statement to handle everything. Any suggestions on that? LinksUp Hall of Fame Group: General Forum Members Points: 3165 Visits: 4946 smrobin (9/13/2013)Thanks for the reply - the problem I have is that I don't always know what the number will be. I have the rules to which I apply to the numbers.Not sure who you are talking to!Since you did not provide any dll or sample data, it had to be made up. Both solutions are generic in that you have to modify either of them to use your table and your column names. Also, both solutions are rounding up whatever is passed to them. There is no "fixed" value. I was hoping to write a simple case statement to handle everything. Is that not what I posted? __________________________________________________________________________________________________________How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/ Sean Lange SSC Guru Group: General Forum Members Points: 149945 Visits: 18581 Edited my code...but the edit was incorrect. :-D _______________________________________________________________Need help? Help us help you. Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.Need to split a string? Try Jeff Modens splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2) smrobin Mr or Mrs. 500 Group: General Forum Members Points: 580 Visits: 136 Thanks for the replies guy! I used your combined logic and got it to work! Thanks again - love this site! Jeff Moden SSC Guru Group: General Forum Members Points: 511959 Visits: 44306 smrobin (9/16/2013)Thanks for the replies guy! I used your combined logic and got it to work! Thanks again - love this site!Two way street here, my friend. Please post what you ended up with. Thanks. --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.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Don Halloran Hall of Fame Group: General Forum Members Points: 3057 Visits: 1804 select ceiling(floor(@somevalue) / 5.0) * 5.0 Blog on sqlservercentral LinksUp Hall of Fame Group: General Forum Members Points: 3165 Visits: 4946 Don Halloran (9/18/2013)select ceiling(floor(@somevalue) / 5.0) * 5.0A MUCH better solution! __________________________________________________________________________________________________________How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

## 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      Security and Auditing 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