{"id":659,"date":"2009-01-09T16:16:00","date_gmt":"2009-01-09T16:16:00","guid":{"rendered":"\/blogs\/bobb\/post\/SQL-Server-Spatial-EMPTY-vs-NULL.aspx"},"modified":"2009-01-09T16:16:00","modified_gmt":"2009-01-09T16:16:00","slug":"sql-server-spatial-empty-vs-null","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/","title":{"rendered":"SQL Server Spatial: EMPTY vs. NULL"},"content":{"rendered":"<p>\nYesterday a friend asked me if it was better to always use a value of GEOMETRYCOLLECTION EMPTY as an alternative to making&nbsp;a geometry\/geography&nbsp;column nullable. OGC has its own concept of database NULL, that is [GEOMETRY] EMPTY, where [GEOMETRY] can be any valid geometry subtype (that is &#39;POINT EMPTY&#39;, &#39;LINESTRING EMPTY&#39;, etc). When I asked why it mattered, he said he was tired of putting ..WHERE&#8230;IS NOT NULL in every UPDATE of SQL Server spatial data type. Sounds like reason for&nbsp;a blog entry.\n<\/p>\n<p>\nNULL is a valid value of any SQL Server data type, defining a variable without initializing it makes it NULL.<br \/>\nDECLARE @x int; &#8212; value of @x is NULL<br \/>\nDECLARE @geog Geography &#8212; value of @geog is NULL\n<\/p>\n<p>\nAccording to SQL-1999 (and above) a SQL data type can have properties and methods; accessor methods and mutator methods. Calling a accessor method on a NULL variable returns NULL. Calling a mutator method on a NULL variable is an error, in SQL Server the error text is &quot;Mutator &#8230; cannot be called on a NULL value&quot;. If I have a table of 100000 geography instances and one row contains NULL, attempting to update that column without using WHERE..IS NOT NULL produces this error.\n<\/p>\n<p>\nUPDATE dbo.Geotable SET geog.STSrid = 4326; &#8212; error if any row contains NULL geog\n<\/p>\n<p>\nEMPTY GEOGRAPHY is a different concept then NULL&nbsp;because OGC defines a common architecture for geography, independent of SQL. In fact, the &quot;OpenGIS&reg; Implementation Specification for Geographic information &#8211; Simple feature access &#8211; Part 1:Common architecture&quot; spec does not even use the word NULL once. EMPTY can be tested with the OGC standard method IsEmpty (STIsEmpty in SQL Server), and is defined as &quot;the empty point set &empty; for the coordinate space&quot;. Because it is not database NULL, you can call methods on it without error:\n<\/p>\n<p>\nDECLARE @a geometry = &#39;POINT EMPTY&#39;;<br \/>\nSET @a.STSrid = 4326; &#8212; works fine\n<\/p>\n<p>\nSome other interesting behaviors of [GEOGRAPHY] EMPTY\n<\/p>\n<p>\ndeclare @a geometry, @b geometry;<br \/>\nset @a = &#39;POINT EMPTY&#39;;<br \/>\nset @b = &#39;LINESTRING EMPTY&#39;;\n<\/p>\n<p>\nselect @a.STEquals(@b);&nbsp; &#8212; true\n<\/p>\n<p>\nselect @a.STDimension(); &#8212; -1 (negative one)\n<\/p>\n<p>\nselect @a.STIsEmpty();&nbsp; &#8212; true, of course<br \/>\nselect @a.STIsSimple(); &#8212; true\n<\/p>\n<p>\n&#8212; Disjoint is true if &quot;Intersection of @a and @b is the empty set&quot;<br \/>\nselect @a.STDisjoint(@b); &#8212; 1\n<\/p>\n<p>\n&#8212; Both Within and Contains are false<br \/>\nselect @a.STWithin(@b);&nbsp; &#8212; 0<br \/>\nselect @a.STContains(@b);&nbsp; &#8212; 0\n<\/p>\n<p>\nSome other interesting uses of [GEOGRAPHY] EMPTY\n<\/p>\n<p>\n&#8212; the boundary of a point is the empty set<br \/>\ndeclare @x geometry = &#39;POINT(1 1)&#39;;<br \/>\nselect @x.STBoundary().ToString();&nbsp; &#8212; GEOMETRYCOLLECTION EMPTY\n<\/p>\n<p>\n&#8212; but the boundary of a polygon is a closed linestring<br \/>\ndeclare @y geometry = &#39;POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))&#39;;<br \/>\nselect @y.STBoundary().ToString();&nbsp; &#8212; &#39;LINESTRING (1 1, 2 1, 2 2, 1 2, 1 1)&#39;\n<\/p>\n<p>\n&#8212; and the boundary of a closed linestring (ring) is GEOMETRYCOLLECTION EMPTY<br \/>\ndeclare @z geometry = @y.STBoundary();<br \/>\nselect @z.STBoundary().ToString(); &#8212; GEOMETRYCOLLECTION EMPTY\n<\/p>\n<p>\nOGC&#39;s SQL Option spec (and SQL Server) differenciates between NULL and [GEOMETRY] EMPTY. For example, in the case above, the boundary of any NULL geometry IS NULL, not EMPTY.\n<\/p>\n<p>\nShould you use NULL or EMPTY as a Geometry\/Geography default? Depends on the semantics that you want to enforce, but, to me, lack of a value in SQL is NULL. And you can even include a constraint that the column value may not be either or both special values. EMPTY is a good way to ensure not having to use &quot;IS NOT NULL&quot; in every UPDATE statement, but that&#39;s as far as it goes.\n<\/p>\n<p>\nWhat do you think?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday a friend asked me if it was better to always use a value of GEOMETRYCOLLECTION EMPTY as an alternative to making&nbsp;a geometry\/geography&nbsp;column nullable. OGC has its own concept of database NULL, that is [GEOMETRY] EMPTY, where [GEOMETRY] can be any valid geometry subtype (that is &#39;POINT EMPTY&#39;, &#39;LINESTRING EMPTY&#39;, etc). When I asked why [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,36],"tags":[],"class_list":["post-659","post","type-post","status-publish","format-standard","hentry","category-sql-server-2008","category-sql-server-spatial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Yesterday a friend asked me if it was better to always use a value of GEOMETRYCOLLECTION EMPTY as an alternative to making&nbsp;a geometry\/geography&nbsp;column nullable. OGC has its own concept of database NULL, that is [GEOMETRY] EMPTY, where [GEOMETRY] can be any valid geometry subtype (that is &#039;POINT EMPTY&#039;, &#039;LINESTRING EMPTY&#039;, etc). When I asked why [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2009-01-09T16:16:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/\",\"name\":\"SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2009-01-09T16:16:00+00:00\",\"dateModified\":\"2009-01-09T16:16:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server 2008\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2008\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server Spatial: EMPTY vs. NULL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin","og_description":"Yesterday a friend asked me if it was better to always use a value of GEOMETRYCOLLECTION EMPTY as an alternative to making&nbsp;a geometry\/geography&nbsp;column nullable. OGC has its own concept of database NULL, that is [GEOMETRY] EMPTY, where [GEOMETRY] can be any valid geometry subtype (that is &#39;POINT EMPTY&#39;, &#39;LINESTRING EMPTY&#39;, etc). When I asked why [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/","og_site_name":"Bob Beauchemin","article_published_time":"2009-01-09T16:16:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/","name":"SQL Server Spatial: EMPTY vs. NULL - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2009-01-09T16:16:00+00:00","dateModified":"2009-01-09T16:16:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/sql-server-spatial-empty-vs-null\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"SQL Server 2008","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sql-server-2008\/"},{"@type":"ListItem","position":3,"name":"SQL Server Spatial: EMPTY vs. NULL"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=659"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}