Distance
DISTANCE(g1 Geometry, g2 Geometry)
Alias: ST_DISTANCE
Returns the shortest distance between g1 and g2.
The Geometry inputs for distance can be any combination of 2D or 3D geometries. If both geometries are 3D then a 3D distance is computed. If both geometries are 2D then a 2D distance is computed. If one geometry is 2D and the other is 3D then a 2D distance is computed (as if the 2D object was infinitely extended along the Z axis).
Result type: float
Examples:
Calculate the distance between two points:
SELECT DISTANCE(POINTFROMTEXT('POINT (-1 -1)'), POINTFROMTEXT('POINT (4 4)')) AS distance;
-----------
distance
-----------
7.071
-----------
g1 and g2 are points:
SELECT DISTANCE(POINTFROMTEXT('POINT(1 0)'), POINTFROMTEXT('POINT(1 5)'));
-----------
col1
-----------
5.000
-----------
g1 and g2 are triangles:
SELECT DISTANCE(POLYFROMTEXT('POLYGON((0 0, 1 2, 2 0, 0 0))'), POLYFROMTEXT('POLYGON((3 0, 3 2, 5 0, 3 0))'));
-----------
col1
-----------
1.000
-----------
Last modified date: 01/30/2023