Click here to monitor SSC
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
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10093 Visits: 1
Comments posted to this topic are about the item Finding Ratio between two numbers
doronyaary
doronyaary
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 145
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-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 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
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8680 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