一,
BEGIN
set @num=6378.138*2*ASIN(SQRT(POW(SIN((lat1*PI()/180-lat2*PI()/180)/2),2)+COS(lat1*PI()/180)*COS(lat2*PI()/180)*POW(SIN((lng1*PI()/180-lng2*PI()/180)/2),2)))*1000;
RETURN @num;
END
二,

--里面0占位符是当前位置纬度 1占位符是当前位置经度
t_Latitude数据库字段纬度 t_Longitude数据库字段经度

ACOS(SIN(({0} * 3.1415) / 180 ) *SIN((CONVERT(NUMERIC(19,9),t_Latitude) * 3.1415) / 180 ) +COS(({0} * 3.1415) / 180 ) * COS((CONVERT(NUMERIC(19,9),t_Latitude) * 3.1415) / 180 ) *COS(({1} * 3.1415) / 180 - (CONVERT(NUMERIC

(19,9),t_Longitude) * 3.1415) / 180 ) ) * 6380
三,
USE [ybd_mbb]
GO

/****** Object:  UserDefinedFunction [dbo].[fnGetDistance]    Script Date: 03/02/2018 09:13:35 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create FUNCTION [dbo].[fnGetDistance](@LatBegin REAL, @LngBegin REAL, @LatEnd REAL, @LngEnd REAL) RETURNS FLOAT
  AS
BEGIN
  --距离(千米)
  DECLARE @Distance REAL
  DECLARE @EARTH_RADIUS REAL
  SET @EARTH_RADIUS = 6378.137
  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
  --SET @Distance = Round(@Distance * 10000) / 10000
  RETURN @Distance
END
GO