1CREATE FUNCTION dbo.RadiusFunc
2
3(
4 @ZipCode nchar(5),
5 @Miles decimal(18, 9)
6)
7
8RETURNS
9 @MaxLongLats TABLE
10 (
11 Latitude decimal(10,8),
12 Longitude decimal(11,8),
13 MaxLatitude decimal(10,8),
14 MinLatitude decimal(10,8),
15 MaxLongitude decimal(11,8),
16 MinLongitude decimal(11,8)
17 )
18AS
19
20BEGIN
21 --Declare variables
22 DECLARE @Latitude decimal(10,8), @Longitude decimal(11,8)
23 DECLARE @MaxLatitude decimal(10, 8), @MinLatitude decimal(10, 8)
24 DECLARE @MaxLongitude decimal(11, 8), @MinLongitude decimal(11, 8)
25
26 --Get the Lat/Long of the zipcode
27 SELECT @Latitude = Latitude, @Longitude = Longitude
28 FROM dbo.ZipCodes
29 WHERE ZipCode = @ZipCode
30
31 --Zipcode not found?
32 IF @@ROWCOUNT = 0
33 RETURN
34
35 --Determine the maxes (69.17 is the # of miles/degree)
36 SET @MaxLatitude = @Latitude + @Miles / 69.17
37 SET @MinLatitude = @Latitude - (@MaxLatitude - @Latitude)
38 SET @MaxLongitude = @Longitude + @Miles / (COS(@MinLatitude * PI() / 180) * 69.17)
39 SET @MinLongitude = @Longitude - (@MaxLongitude - @Longitude)
40
41 --Insert data into return table
42 INSERT INTO @MaxLongLats
43 (Latitude, Longitude, MaxLatitude, MinLatitude, MaxLongitude, MinLongitude)
44 SELECT @Latitude, @Longitude, @MaxLatitude, @MinLatitude, @MaxLongitude, @MinLongitude
45 RETURN
46END