SQLServerCentral.com / Article Discussions / Article Discussions by Author / Discuss content posted by SRIKANTH & SUBIN / Finding Ratio between two numbers / Latest PostsInstantForum.NET v2.9.0SQLServerCentral.comhttp://www.sqlservercentral.com/Forums/notifications@sqlservercentral.comThu, 26 Mar 2015 17:07:37 GMT20RE: Finding Ratio between two numbershttp://www.sqlservercentral.com/Forums/Topic931786-2709-1.aspxHi,You might like to consider using Euclid's Algorithm to find the Highest Common Factor (Greatest Common Divisor)[code="sql"]CREATE FUNCTION CalculateRatio(@numerator int, @denominator int)RETURNS nvarchar(max)ASBEGIN declare @ratio nvarchar(max) declare @greatestcommondivisor int declare @x int declare @y int set @x = @numerator set @y = @denominator declare @tmp int if (@denominator > 0) begin while (@x > 0) begin if @x < @y begin set @tmp = @x set @x = @y set @y = @tmp end set @x = @x % @y end end set @greatestcommondivisor = @y if (@numerator = 0 or @denominator = 0 or @greatestcommondivisor = 1) begin set @ratio = convert(nvarchar(max), @numerator) + ':' + convert(nvarchar(max), @denominator) end else begin set @ratio = convert(nvarchar(max), @numerator/@greatestcommondivisor) + ':' + convert(nvarchar(max), @denominator/@greatestcommondivisor) end return @ratioEND[/code]It returns a result faster than the original (even being lazy and using nvarchar(max)), much faster for larger numberscheersWed, 23 Jun 2010 04:06:56 GMTPete CoxRE: Finding Ratio between two numbershttp://www.sqlservercentral.com/Forums/Topic931786-2709-1.aspxThank you for your code. It was very helpful.By the way... I`m looking for a code to view recent T-SQL that was executed. Just like SQL Profiler but I want to incorporate it in my application. Do you know how to do that or where can I find further help ?Thanks !Doron.Wed, 02 Jun 2010 23:29:40 GMTdoronyaaryFinding Ratio between two numbershttp://www.sqlservercentral.com/Forums/Topic931786-2709-1.aspxComments posted to this topic are about the item [B]<A HREF="/scripts/SQL/70395/">Finding Ratio between two numbers</A>[/B]Wed, 02 Jun 2010 20:11:36 GMTSite Owners