Calculate distance in SQL Server based on map latitude and longitude

Source: Internet
Author: User
Tags sin

Baked from somewhere else, you try it out, you can calculate it, but the correct is not quite understand.

Use [Niaoren]
GO
/****** object:userdefinedfunction [dbo].    [Fngetdistance] Script DATE:2017/7/28 Friday 12:17:56 ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO
--Calculates the distance between two coordinate points (longitude, latitude) of the Earth SQL function
--lordbaby
--Finishing: www.aspbc.com
ALTER FUNCTION [dbo]. [Fngetdistance] (@LatBegin Real, @LngBegin Real, @LatEnd Real, @LngEnd Real) RETURNS FLOAT
As
BEGIN
--distance (km)
DECLARE @Distance REAL
DECLARE @EARTH_RADIUS REAL
SET @EARTH_RADIUS = 6378.137-Earth radius
DECLARE @RadLatBegin Real, @RadLatEnd Real, @RadLatDiff real, @RadLngDiff Real
SET @RadLatBegin = @LatBegin *pi ()/180.0
SET @RadLatEnd = @LatEnd *pi ()/180.0
SET @RadLatDiff = @RadLatBegin-@RadLatEnd
SET @RadLngDiff = @LngBegin *pi ()/180.0-@LngEnd *pi ()/180.0
SET @Distance = 2 *asin (SQRT (POWER (SIN (@RadLatDiff/2), 2) +cos (@RadLatBegin) *cos (@RadLatEnd) *power (SIN (@RadLngDiff/2 ), 2)))
SET @Distance = @Distance * @EARTH_RADIUS
RETURN @Distance
END

Original address: http://www.open-open.com/code/view/1436452727411

Calculate distance in SQL Server based on map latitude and longitude

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.