Need to edge out the competition for your dream job? Train for certifications today.
Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
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
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.
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