SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding Ratio between two numbers


Finding Ratio between two numbers

Author
Message
Site Owners
Site Owners
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: Administrators
Points: 15889 Visits: 129
Comments posted to this topic are about the item Finding Ratio between two numbers
doronyaary
doronyaary
Valued Member
Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)Valued Member (56 reputation)

Group: General Forum Members
Points: 56 Visits: 148
Thank 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.
Pete Cox
Pete Cox
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 Visits: 277
Hi,
You might like to consider using Euclid's Algorithm to find the Highest Common Factor (Greatest Common Divisor)


CREATE FUNCTION CalculateRatio(@numerator int, @denominator int)
RETURNS nvarchar(max)
AS
BEGIN

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 @ratio

END



It returns a result faster than the original (even being lazy and using nvarchar(max)), much faster for larger numbers

cheers
Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32052 Visits: 885
Thanks for the script.
Go


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







































































































































































SQLServerCentral


Search