You have to respect all the syntax rules, and here we go:

CREATE FUNCTION dbo.geo_distance (@lon1 decimal, @lat1 decimal, @lon2 decimal, @lat2 decimal)

RETURNS decimal

AS

BEGIN

DECLARE @dist decimal

IF (@lat1 = @lat2 AND @lon1 = @lon2)

BEGIN

SET @dist = 0

END

ELSE

IF (SIN(@lat1/57.29577951)*SIN(@lat2/57.29577951) + COS(@lat1/57.29577951)*COS(@lat2/57.29577951)*COS((@lon1/57.29577951)-(@lon2/57.29577951))) > 1

BEGIN

SET @dist = (3963.1*ACOS(1)) * 1.6094

END

ELSE

BEGIN

SET @dist = (3963.1 * ACOS(SIN(@lat1/57.29577951)*SIN(@lat2/57.29577951) + COS(@lat1/57.29577951)*COS(@lat2/57.29577951)*COS((@lon1/57.29577951)-(@lon2/57.29577951)))) * 1.6094

END

RETURN @dist

END

go

select dbo.geo_distance ( 1,2,3,4)

go

drop function dbo.geo_distance

CHeers

CREATE FUNCTION dbo.geo_distance (@lon1 decimal, @lat1 decimal, @lon2 decimal, @lat2 decimal)

RETURNS decimal

AS

BEGIN

DECLARE @dist decimal

IF (@lat1 = @lat2 AND @lon1 = @lon2)

BEGIN

SET @dist = 0

END

ELSE

IF (SIN(@lat1/57.29577951)*SI

BEGIN

SET @dist = (3963.1*ACOS(1)) * 1.6094

END

ELSE

BEGIN

SET @dist = (3963.1 * ACOS(SIN(@lat1/57.29577951

END

RETURN @dist

END

go

select dbo.geo_distance ( 1,2,3,4)

go

drop function dbo.geo_distance

CHeers