Add or subtract Fractions in sql server 2005

  • I am looking for a way to add or subtract fractions stored  as 
    2/3 , 7/5 or 2/7 , -5/6 etc 
    in sql server 2005 .
    The output should be in fractions only as 1/4 + 2/4 = 3/4

  • shiwani.plrs - Monday, March 19, 2018 5:13 AM

    I am looking for a way to add or subtract fractions stored  as 
    2/3 , 7/5 or 2/7 , -5/6 etc 
    in sql server 2005 .
    The output should be in fractions only as 1/4 + 2/4 = 3/4

    Does this look like what you need?
    I'm assuming that you are able to code the arithmetic part & just need help converting that to a fraction afterwards.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Using fractions like 1/3 or even 1/10 is always risky when using float calculations. Both 1/3 and 1/10 are not exactly presented in most binairy systems.

    A1, A2, B1, B2 all being integers. Calculations can be done with integers (or big ints) only.
    So a sum like :  A1/B1 + A2/B2
    Calculate :
       Numerator:  (A1*B2 + A2*B1)
       Divisor:    (B1 * B2)
    Both Numerator and Diviser should be devided by their GCD. (Greatest Common Divider).

    For a function for the GCD see :

    GCD function 1
    GCD function 2

    You'll probably be able to find more GCD functions on line, which better conforms to your problem.

    For very large numbers 
    Numerator:  (A1*B2 / (GCD(B1,B2) + A2*B1/ (GCD(B1,B2))
    Divisor:    (B1 * B2)/ (GCD(B1,B2)

    And then divided by the GCD of both results.

    Ben

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply