Your technology certification is waiting. Enroll in Cloud Class ®
CREATE FUNCTION [dbo].[fnDistance]
@Lat1 decimal(11, 8),
@Lng1 decimal(11, 8),
@Lat2 decimal(11, 8),
@Lng2 decimal(11, 8)
RETURNS decimal(10, 6) -- max 10km
if abs(@Lat2-@Lat1) > 1 or abs(@Lng2-@Lng1) > 1
declare @distance decimal(30, 8)
declare @R as int
select @R = 6371000
declare @dLat as decimal(21, 17)
declare @dLng as decimal(21, 17)
select @dLat = (@Lat2-@Lat1) * pi() / 180
select @dLng = (@Lng2-@Lng1) * pi() / 180
declare @a as decimal(18, 18)
declare @c as decimal(18, 18)
select @a = Sin(@dLat / 2) * Sin(@dLat / 2) +
Cos(@Lat1 * pi() / 180) * Cos(@Lat2 * pi() / 180) * Sin(@dLng /2) * Sin(@dLng / 2);
select @c = 2 * Atn2(Sqrt(@a), Sqrt(1-@a))
select @distance = (@R * @c )/1000-- % 10000
Open in new window
@UserLong decimal (11,8)=-81.243822,
@radius int =1500 -- radius should be in meters
[dbo].[fnDistance](SupplierLat, SupplierLong, @UserLat, @UserLong)<@radius
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.