Introduction
Spatial data types in SQL allow you to store and manipulate geographic information. This topic provides an overview of spatial data types and their use in SQL Server.
Geography Data Types
The geography data type represents points, lines, and areas in two-dimensional space. It is based on the ellipsoidal model of the Earth.
Common geography data types include:
- geometry: A basic point or area, representing a shape.
- geography: A more precise representation of a shape based on the ellipsoid.
- geography_point: A point, representing a single location.
- geography_polygon: A polygon, representing a two-dimensional area.
- geography_line: A line, representing a path or boundary.
Geometry Data Types
The geometry data type represents points and areas in two-dimensional space. It is based on a planar model of the Earth, which is a simpler representation than the geography data type.
Common geometry data types include:
- geometry: A basic point or area, representing a shape.
- geometry_point: A point, representing a single location.
- geometry_polygon: A polygon, representing a two-dimensional area.
Spatial Functions
Spatial functions are built-in SQL functions that perform spatial operations, such as calculating distances, finding intersections, and determining containment.
Some example spatial functions include:
- ST_Distance
- ST_Intersects
- ST_Contains
Example: Calculating Distance Between Two Points
SELECT ST_Distance(geography_point1, geography_point2) AS distance
FROM my_table;