locked
Distance between two points lat/long RRS feed

  • Question

  • I have a user defined function, I want to determine the distance between the 2 points. I have it working but i'm having a problem getting to print.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Code Snippet
    create function dbo.Distance( @lat1 float , @long1 float , @lat2 float , @long2 float)
    returns float

    as

    begin

    declare @DegToRad as float
    declare @Ans as float
    declare @Miles as float

    set @DegToRad = 57.29577951
    set @Ans = 0
    set @Miles = 0

    if @lat1 is null or @lat1 = 0 or @long1 is null or @long1 = 0 or @lat2 is
    null or @lat2 = 0 or @long2 is null or @long2 = 0

    begin

    return ( @Miles )

    end

    set @Ans = SIN(@lat1 / @DegToRad) * SIN(@lat2 / @DegToRad) + COS(@lat1 / @DegToRad ) * COS( @lat2 / @DegToRad ) * COS(ABS(@long2 - @long1 )/@DegToRad)

    set @Miles = 3959 * ATAN(SQRT(1 - SQUARE(@Ans)) / @Ans)

    set @Miles = CEILING(@Miles)

    return ( @Miles )

    end

    DECLARE @RC float
    EXEC Distance '39.943762', '-78.122265', '32.334709', '-96.633546'
    PRINT @RC /* in miles */

     

     



    Saturday, January 12, 2008 8:01 AM

Answers

  • Thanks for the help it turned out to be.

     

    DECLARE @RC float

    EXEC @RC = Distance '39.943762', '-78.122265', '32.334709', '-96.633546'

    PRINT @RC /* in miles */

     

    Sunday, January 13, 2008 4:56 PM

All replies

  • Return the value to a variable, e.g.

     

    DECLARE @RC float
    EXEC @RC = GetDistanceBetween2Points '39.943762', '-78.122265', '32.334709', '-96.633546'
    PRINT @RC /* in miles */

    Sunday, January 13, 2008 5:49 AM
  • Thanks for the help it turned out to be.

     

    DECLARE @RC float

    EXEC @RC = Distance '39.943762', '-78.122265', '32.334709', '-96.633546'

    PRINT @RC /* in miles */

     

    Sunday, January 13, 2008 4:56 PM