select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

IF OBJECT_ID ('dbo.SpatialTable') IS NOT NULL DROP TABLE dbo.SpatialTable;GOCREATE TABLE SpatialTable (Cell CHAR(2), GeomCol GEOMETRY);GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('A1', 'POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('A2', 'POLYGON ((101 0, 200 0, 200 100, 101 100, 101 0))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('B1', 'POLYGON ((0 101, 100 101, 100 200, 0 200, 0 101))');GOINSERT INTO SpatialTable (Cell, GeomCol)VALUES ('B2', 'POLYGON ((101 101, 200 101, 200 200, 101 200, 101 101))');GODECLARE @Point GEOMETRY;SET @Point = 'POINT (25 130)'SELECT CellFROM SpatialTableWHERE GeomCol.STIntersects(@Point) = 1;SET @Point = 'POINT (105 40)'SELECT CellFROM SpatialTableWHERE GeomCol.STIntersects(@Point) = 1;

WITH Squares ( Upperleft_X, Upperleft_Y, Upperright_X, Upperright_Y ,Lowerleft_X, Lowerleft_Y, Lowerright_X, Lowerright_Y, Cell) AS ( SELECT 0, 0, 100, 0, 0, 100, 100, 100, 'A1' UNION ALL SELECT 0, 101, 100, 101, 0, 200, 100, 200, 'B1' UNION ALL SELECT 101, 0, 200, 0, 101, 100, 200, 100, 'A2' UNION ALL SELECT 101, 101, 200, 101, 101, 200, 200, 200, 'B2'),Points (n, x, y) AS ( SELECT 1, 25, 130 UNION ALL SELECT 2, 105, 40)SELECT a.n, b.Cell, x, y, Upperleft_X, Upperright_X, Lowerleft_X, Lowerright_X , Upperleft_Y, Upperright_Y, Lowerleft_Y, Lowerright_YFROM Points aJOIN Squares b ON (x BETWEEN Upperleft_X AND Lowerright_X AND y BETWEEN Upperleft_Y AND Lowerright_Y);

CREATE TABLE Squares (CellNo CHAR(2) NOT NULL, TopLeftX INT NOT NULL, TopLeftY INT NOT NULL, BottomRightX INT NOT NULL, BottomRightY INT NOT NULL);INSERT INTO Squares VALUES ('A1', 0, 0, 100, 100), ('A2', 0, 101, 100, 200), ('B1', 101, 0, 200, 100), ('B2', 101, 101, 200, 200);

ALTER TABLE SquaresADD CONSTRAINT SquaresCoordinates UNIQUE CLUSTERED (TopLeftX, TopLeftY, BottomRightX, BottomRightY);

SELECT *FROM SquaresWHERE (25 BETWEEN TopLeftX AND BottomRightX AND 130 BETWEEN TopLeftY AND BottomRightY)OR (105 BETWEEN TopLeftX AND BottomRightX AND 40 BETWEEN TopLeftY AND BottomRightY);