All Products
Search
Document Center

ApsaraDB RDS:ST_Distance

Last Updated:Mar 28, 2026

ST_Distance — Returns the distance between two geometry or geography values.

Syntax

float ST_Distance(geometry g1, geometry g2);
float ST_Distance(geography gg1, geography gg2);
float ST_Distance(geography gg1, geography gg2, boolean useSpheroid);

Parameters

ParameterTypeDescription
g1geometryThe first geometry object.
g2geometryThe second geometry object.
gg1geographyThe first geography object.
gg2geographyThe second geography object.
useSpheroidbooleanSpecifies whether to use an ellipsoid for distance calculation. Default: true. Set to false to use a faster spherical calculation.

Description

For geometry types, returns the minimum 2D Euclidean distance between two geometry objects, in the units of their coordinate reference system (CRS).

For geography types, returns the minimum spherical distance in meters between the geography objects. If useSpheroid is false, a faster spherical calculation is used instead.

This function supports circular strings and curves.

Examples

Compare geometry and 3D distance

The following example shows the difference between ST_Distance and ST_3DDistance. For a 3D point (0,0,0) to (1,1,1), ST_Distance calculates the 2D planar distance while ST_3DDistance accounts for all three dimensions.

SELECT ST_Distance(g1, g2), ST_3DDistance(g1, g2)
FROM (
  SELECT 'POINT(0 0 0)'::geometry AS g1,
         'POINT(1 1 1)'::geometry AS g2
) AS test;

Output:

   st_distance   |  st_3ddistance
-----------------+------------------
 1.4142135623731 | 1.73205080756888
(1 row)