title: Calculating distance between 2 points. Can I trust in GeoLocation.STDistance function of sql server?
body: <p>I want to calculate the distance between two points with SQL server. The problem is that I get a result that is not accurate.</p>
  
  <pre><code>Create table #auxdist
  (    idempr                 int identity(1,1)
  ,emp_desc               varchar(60) 
  ,longitud               float
  ,latitud                float
  ,geolocation            geography
  )
  
  insert into #auxdist values ('TIng',-55.769,-34.768,NULL)
  insert into #auxdist values ('CComp',-55.74132,-34.19647,NULL)
  
  Update  #auxdist
  SET  [GeoLocation] = geography::STPointFromText('POINT(' + cast( CAST(cs.latitud   AS decimal(30,20)) as varchar(30)) + ' ' + 
          cast( CAST(cs.longitud AS decimal(30,20)) as varchar(30))+ ')', 4326) 
   from  #auxdist cs 
  
  select   com1.idempr,com2.idempr,           com1.GeoLocation.STDistance(com2.GeoLocation) as dist
   from    #auxdist com1
   join    #auxdist com2 on com1.idempr &lt;&gt; com2.idempr  and com1.idempr=1
  </code></pre>
  
  <p>Using this query I get that the distance between two points is 36016 mts.
  Then I check the result using <a href="http://www.onlineconversion.com/map_greatcircle_distance.htm" rel="nofollow noreferrer">http://www.onlineconversion.com/map_greatcircle_distance.htm</a>
  and I found a different answer: 63673mts (Haversine formula)</p>
  
  <p><img src="https://i.stack.imgur.com/TVHvI.png" alt="Distance"></p>