{"id":497,"date":"2020-03-18T02:06:41","date_gmt":"2020-03-17T13:06:41","guid":{"rendered":"https:\/\/blog.wiseowls.co.nz\/?p=497"},"modified":"2026-03-08T00:44:47","modified_gmt":"2026-03-07T11:44:47","slug":"sql-server-computed-columns-not-just-mathematics","status":"publish","type":"post","link":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/","title":{"rendered":"SQL Server computed columns: not just mathematics"},"content":{"rendered":"\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/alter-table-computed-column-definition-transact-sql?view=sql-server-ver15#arguments\">Computed columns<\/a> in SQL server are very handy when we&#8217;ve got a formula that we can rely on and some CPU cycles to spare. There&#8217;s heaps of articles online about what these are, so we&#8217;d not repeat it here. Let us rather look at one peculiar case&#8230;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What if I&#8217;ve got two bit columns?<\/h2>\n\n\n\n<p>A very plausible scenario indeed. Let&#8217;s create a table like so: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE Test (\nId INT PRIMARY KEY,\nCheck1 BIT NOT NULL,\nCheck2 BIT NOT NULL,\nCheckAll AS (Check1 = 1 OR Check2 = 1) -- Incorrect syntax near '='.\n)<\/code><\/pre>\n\n\n\n<p>that didn&#8217;t seem to work and the error message is a bit unhelpful: we know it&#8217;s wrong but why?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reading the documentation once again<\/h2>\n\n\n\n<p><em>computed_column_expression<\/em><br>Is an expression that <span class=\"su-highlight\" style=\"background:#f1fc1a;color:#000000\">&nbsp;defines the value&nbsp;<\/span> of a computed column. A computed column is a virtual column that is not physically stored in the table but is computed from an expression that uses other columns in the same table. For example, a computed column could have the definition: cost AS price * qty. The <span class=\"su-highlight\" style=\"background:#f1fc1a;color:#000000\">&nbsp;expression can be a noncomputed column name, constant, function, variable&nbsp;<\/span>, and any combination of these connected by one or more operators. The expression cannot be a subquery or include an alias data type.<\/p>\n\n\n\n<p>Okay,  this kinda hints at the fact we need to return a value.  So a simple search condition does not cut it. Let us try one more time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE Test (\nId INT PRIMARY KEY,\nCheck1 BIT NOT NULL,\nCheck2 BIT NOT NULL,\nCheckAll AS (CASE WHEN Check1 = 1 OR Check2 = 1 THEN 1 ELSE 0 END) -- this time is better! we can roll with it\n)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Take it up another notch<\/h2>\n\n\n\n<p>The above example works but looks a bit too verbose and explicit. Is there a better way? If we deal with <strong>bit<\/strong> columns, we might be in luck and opt for <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/language-elements\/bitwise-or-transact-sql?view=sql-server-ver15\">t-sql bitwise operators<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE Test (\nId INT PRIMARY KEY,\nCheck1 BIT NOT NULL,\nCheck2 BIT NOT NULL,\nCheckAll AS (Check1|Check2) \n)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Computed columns in SQL server are very handy when we&#8217;ve got a formula that we can rely on and some CPU cycles to spare. There&#8217;s heaps of articles online about what these are, so we&#8217;d not repeat it here. Let us rather look at one peculiar case&#8230; What if I&#8217;ve got two bit columns? A &hellip; <a href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server computed columns: not just mathematics&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[51],"tags":[59,39],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-database","tag-sql","tag-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server computed columns: not just mathematics - Timur and associates<\/title>\n<meta name=\"description\" content=\"SQL Server computed columns with bit fields don&#039;t work the way you&#039;d expect. The workaround is CASE expressions and CAST.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server computed columns: not just mathematics - Timur and associates\" \/>\n<meta property=\"og:description\" content=\"SQL Server computed columns with bit fields don&#039;t work the way you&#039;d expect. The workaround is CASE expressions and CAST.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/\" \/>\n<meta property=\"og:site_name\" content=\"Timur and associates\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-17T13:06:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-07T11:44:47+00:00\" \/>\n<meta name=\"author\" content=\"timur\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TimurKh\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/\"},\"author\":{\"name\":\"timur\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"headline\":\"SQL Server computed columns: not just mathematics\",\"datePublished\":\"2020-03-17T13:06:41+00:00\",\"dateModified\":\"2026-03-07T11:44:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/\"},\"wordCount\":269,\"commentCount\":0,\"keywords\":[\"sql\",\"sql server\"],\"articleSection\":[\"Database\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/\",\"name\":\"SQL Server computed columns: not just mathematics - Timur and associates\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\"},\"datePublished\":\"2020-03-17T13:06:41+00:00\",\"dateModified\":\"2026-03-07T11:44:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\"},\"description\":\"SQL Server computed columns with bit fields don't work the way you'd expect. The workaround is CASE expressions and CAST.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/index.php\\\/2020\\\/03\\\/18\\\/sql-server-computed-columns-not-just-mathematics\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server computed columns: not just mathematics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#website\",\"url\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/\",\"name\":\"Timur and associates\",\"description\":\"Notes of an IT contractor\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.wiseowls.co.nz\\\/#\\\/schema\\\/person\\\/34d0ed30d573b5bc317ea990bd2e0c59\",\"name\":\"timur\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg\",\"caption\":\"timur\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/TimurKh\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server computed columns: not just mathematics - Timur and associates","description":"SQL Server computed columns with bit fields don't work the way you'd expect. The workaround is CASE expressions and CAST.","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:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server computed columns: not just mathematics - Timur and associates","og_description":"SQL Server computed columns with bit fields don't work the way you'd expect. The workaround is CASE expressions and CAST.","og_url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/","og_site_name":"Timur and associates","article_published_time":"2020-03-17T13:06:41+00:00","article_modified_time":"2026-03-07T11:44:47+00:00","author":"timur","twitter_card":"summary_large_image","twitter_creator":"@TimurKh","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/#article","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/"},"author":{"name":"timur","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"headline":"SQL Server computed columns: not just mathematics","datePublished":"2020-03-17T13:06:41+00:00","dateModified":"2026-03-07T11:44:47+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/"},"wordCount":269,"commentCount":0,"keywords":["sql","sql server"],"articleSection":["Database"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/","url":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/","name":"SQL Server computed columns: not just mathematics - Timur and associates","isPartOf":{"@id":"https:\/\/blog.wiseowls.co.nz\/#website"},"datePublished":"2020-03-17T13:06:41+00:00","dateModified":"2026-03-07T11:44:47+00:00","author":{"@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59"},"description":"SQL Server computed columns with bit fields don't work the way you'd expect. The workaround is CASE expressions and CAST.","breadcrumb":{"@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.wiseowls.co.nz\/index.php\/2020\/03\/18\/sql-server-computed-columns-not-just-mathematics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.wiseowls.co.nz\/"},{"@type":"ListItem","position":2,"name":"SQL Server computed columns: not just mathematics"}]},{"@type":"WebSite","@id":"https:\/\/blog.wiseowls.co.nz\/#website","url":"https:\/\/blog.wiseowls.co.nz\/","name":"Timur and associates","description":"Notes of an IT contractor","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.wiseowls.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.wiseowls.co.nz\/#\/schema\/person\/34d0ed30d573b5bc317ea990bd2e0c59","name":"timur","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/23d55e17d4f0990ee4d12bc6e5dcfb58a292934efd62a185756876379e780b16?s=96&r=pg","caption":"timur"},"sameAs":["https:\/\/x.com\/TimurKh"]}]}},"_links":{"self":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/comments?post=497"}],"version-history":[{"count":3,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":500,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/posts\/497\/revisions\/500"}],"wp:attachment":[{"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wiseowls.co.nz\/index.php\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}