Willcode4cash icon

Find coordinates within a given radius

Willcode4cash | PRO | 07/03/17 10:25:05 PM UTC | 0 ⭐ | 269 👁️ | Never ⏰ | []
T-SQL |

850 B

|

None

|

0 👍

/

0 👎

-- 60       => minutes/degree
-- 1.1515   => statute miles/nautical mile
-- 1.609344 => km/mile
-- Town table is where your latitude and longitude information are stored
 
DECLARE @Latitude                       FLOAT = 40.646667,
        @Longitude                      FLOAT = -83.608611, 
        @MinutesPerDegree               FLOAT = 60.0, 
        @StatuteMilesPerNauticalMile    FLOAT = 1.1515, 
        @KilometersPerMile              FLOAT = 1.609344,
        @multiplier                     FLOAT = @MinutesPerDegree * @StatuteMilesPerNauticalMile * @KilometersPerMile
 
SELECT * FROM (
    SELECT
        TownID,
        Name,
        (((
            ACOS(
                SIN((@Latitude * pi() / 180)) *
                SIN((Latitude  * pi() / 180)) +
                COS((@Latitude * pi() / 180)) *
                COS((Latitude  * pi() / 180)) *
                COS((@Longitude - Longitude) * pi() / 180))
            ) * 180 / pi()
        ) * @multiplier
    ) AS DistanceKM
    FROM Town
) t
WHERE Distance <= 75

Comments