The ST_DISTANCE function returns the shortest distance between two geography objects in meters.
Syntax
DOUBLE ST_DISTANCE(GEOGRAPHY <value1>, GEOGRAPHY <value2>)Parameters
value1, value2: Required. Values of the GEOGRAPHY data type. You can construct GEOGRAPHY objects by using functions such as ST_GEOGPOINT, ST_GEOGFROMWKB, ST_GEOGFROMTEXT, ST_MAKELINE, and ST_MAKEPOLYGON.
Return value
Returns a DOUBLE value representing the shortest distance between two geography objects in meters. The following rules apply:
If either
value1orvalue2is an empty geography object, the function returns NULL.Due to the precision limitations of the S2 Geometry Library, the returned value may differ slightly from the actual distance.
Examples
Example 1: Calculate the shortest distance between two points
SELECT ST_DISTANCE(ST_GEOGFROMTEXT('POINT(0 0)'), ST_GEOGFROMTEXT('POINT(1 1)')); -- Returns: +-----------------------+ | _c0 | +-----------------------+ | 157249.62809250783 | +-----------------------+Example 2: Handle an empty geography object
-- Returns NULL because the second geography object is empty. SELECT ST_DISTANCE(ST_GEOGFROMTEXT('POINT(0 0)'), ST_GEOGFROMTEXT('POINT EMPTY'));