Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Rounding to nearest 0.05
Message
De
21/05/2018 11:22:23
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
21/05/2018 10:39:28
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2012
Divers
Thread ID:
01660219
Message ID:
01660223
Vues:
50
>Hi,
>I need to be able to round some totals to the nearest 0.05.
>
>I have done this in C# using this code:
>
>
        public static decimal RoundToNearest(decimal value, decimal nearest)
>        {
>            decimal remainder;
>            decimal returnValue;
>
>            remainder = value % nearest;
>            if (remainder >= (nearest/2))
>        	{
>                returnValue = value + (nearest - remainder);
>        	}
>            else
>            {
>                returnValue = value - remainder;
>            }
>            return returnValue;
>        }
>
>Is there a way to do this in SQL Server? Shall I just try to use the same logic in a UDF?
>
>I will need to use it in queries like:
>
>SELECT RoundToNearest(SUM(SomeAmount), 0.05) AS RoundedTotal

As an addition to what Antonio said, you could directly make your C# function a CLR function for SQL server. Then you could call it as if it was a built-in SQL server function (caveat: the function would only available in the database(s) that register the dll. Good side: You can write some complex functions in C#\Vb.Net).
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform