Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  General SQL Server Forums  Script Library  Rounding to NEAREST X.XX Reply to Topic  Printer Friendly
Author  Topic

ccscowboy
Starting Member

USA
1 Posts

 Posted - 12/15/2010 :  10:34:50 we have always had an issue with our customers to round to nearest 1.99 or 5.99 or 0.99 things like that... myself and Glenn a coworker wrote the following Scalar function to solve it, 1 = up, 2 = down, and 3 - nearest-- SQLACTION:RELEASE-- =============================================-- Author: Andy Stapleton-- Create date: 2010-12-15-- Description: RoundNumber to Nearest up or down.-- =============================================CREATE FUNCTION DBO.GET_RoundVal_FN(@Amt Decimal(19,6),@RoundTo Decimal(19,6),@RoundType int )RETURNS Decimal(19,6)ASBEGIN -- Declare the return variable here declare @rp int, @rb int, @RetVal Decimal(19,6), @Dir Varchar(5) set @rp = convert(int,@RoundTo-1) IF @rp < 0 set @rp = 0 --end IF @rp <> 0 set @rp = len(convert(varchar(25),@rp)) set @rb = POWER(10,@rp) -- Base as a power, results in a 1, 10, 100, 1000, etc. set @RetVal = (convert(int,(@Amt / @rb)) * @rb) + @RoundTo IF @RoundType = 1 and @RetVal < @Amt --up set @RetVal = @RetVal + @rb --END IF @RoundType = 2 and @RetVal > @Amt -- down set @RetVal = @RetVal - @rb --END IF @RoundType = 3 and abs(@RetVal - @Amt) > abs((@RetVal-@rb) - @Amt) set @RetVal = @RetVal - @rb --end IF @RetVal < 0 set @RetVal = @RetVal + @rb --END RETURN isNull(@RetVal,0)ENDGO--Select DBO.GET_NearestNo_FN(136.10,0.99,3)Andy <> Stapleton

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 02/10/2011 :  15:24:04 This seems easier to me```CREATE FUNCTION dbo.fnGetRoundVal ( @Amt DECIMAL(19, 6), @RoundTo DECIMAL(19, 6), @RoundType TINYINT ) RETURNS DECIMAL(19, 6) AS BEGIN RETURN ( SELECT CASE WHEN @RoundType = 1 THEN @Amt + Up WHEN @RoundType = 2 THEN @Amt - Down WHEN Down < Up THEN @Amt - Down ELSE @Amt + Up END FROM ( SELECT @Amt % CEILING(@RoundTo) - @RoundTo + CEILING(@RoundTo) AS Down, @RoundTo - @Amt % CEILING(@RoundTo) AS Up ) AS d ) END```N 56°04'39.26"E 12°55'05.63"
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC