The ST_WITHIN function determines whether the geography object value1 is within the geography object value2. This is true if every point of value1 is also a point of value2.
Syntax
BOOLEAN ST_WITHIN(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 BOOLEAN value as follows:
TRUEif every point of the geography objectvalue1is within the geography objectvalue2; otherwise,FALSE.Returns
FALSEif eithervalue1orvalue2is an empty geography object.
A geography object does not include its boundary nodes.
Examples
Check whether the points (5, 5), (20, 20), and (0, 0) are within the polygon defined by POLYGON((0 0, 10 0, 10 10, 0 10, 0 0)).
-- Returns TRUE. POINT(5 5) is inside the polygon.
SELECT ST_WITHIN(ST_GEOGFROMTEXT('POINT(5 5)'), ST_GEOGFROMTEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));
-- Returns FALSE. POINT(20 20) is outside the polygon.
SELECT ST_WITHIN(ST_GEOGFROMTEXT('POINT(20 20)'), ST_GEOGFROMTEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));
-- Returns FALSE. POINT(0 0) is on the polygon boundary.
SELECT ST_WITHIN(ST_GEOGFROMTEXT('POINT(0 0)'), ST_GEOGFROMTEXT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'));