{"id":5587,"date":"2013-05-07T00:00:22","date_gmt":"2013-05-07T00:00:22","guid":{"rendered":"http:\/\/craftydba.com\/?p=5587"},"modified":"2016-04-23T01:00:32","modified_gmt":"2016-04-23T01:00:32","slug":"logical-operators","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5587","title":{"rendered":"Logical Operators"},"content":{"rendered":"<p>I am going to make headway on the series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. <\/p>\n<p>I will exploring the <a href=\" http:\/\/msdn.microsoft.com\/en-us\/library\/ms189773.aspx\">Logical Operators<\/a> today.  In short, these operators return either a boolean TRUE or FALSE.<\/p>\n<p>To demonstrate many of these operators, we need a temporary table to query against.  The Transaction SQL code below creates a table that contains the first eight prime numbers.<\/p>\n<p>Any complex real world programming example probably has one or more of these operators in the TSQL script.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"setup play table\">\r\n--\r\n-- ~ First 8 prime numbers ~\r\n--\r\n\r\n-- Drop existing table\r\nIF EXISTS \r\n(\r\n    SELECT * \r\n    FROM tempdb.sys.objects T \r\n    WHERE T.TYPE = 'U' AND T.name LIKE '#PRIMES%'\r\n) \r\nDROP TABLE #PRIMES;\r\nGO\r\n\r\n-- Make temp table\r\nCREATE TABLE #PRIMES (NUM INT PRIMARY KEY);\r\nGO\r\n\r\n-- Add data to table\r\nINSERT INTO #PRIMES VALUES \r\n    (2), (3), (5), (7),\r\n    (11), (13), (17), (19);\r\n\r\n-- Show the data\r\nSELECT * FROM #PRIMES;\r\n<\/pre>\n<\/p>\n<p>The output of the SELECT statement showing the prime numbers.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nNUM\r\n-----------\r\n2\r\n3\r\n5\r\n7\r\n11\r\n13\r\n17\r\n19\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175064.aspx\">SOME<\/a> operator compares a scalar value with a single column set of values.  It returns TRUE when one or more conditions are satisfied.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"some operator\">\r\nPRINT '13 LESS THAN [SOME] OF THE FIRST 8 PRIMES?' \r\nIF 13 < SOME (SELECT NUM FROM #PRIMES)\r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\n13 LESS THAN [SOME] OF THE FIRST 8 PRIMES?\r\nTRUE\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms175064.aspx\">ANY<\/a> operator is equivalent to the SOME operator.  It returns FALSE when all of the conditions are not satisfied.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"any operator\">\r\nPRINT '23 LESS THAN [ANY] OF THE FIRST 8 PRIMES?' \r\nIF 23 < ANY (SELECT NUM FROM #PRIMES)\r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\n23 LESS THAN [ANY] OF THE FIRST 8 PRIMES?\r\nFALSE\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178543.aspx\">ALL<\/a> operator compares a scalar value with a single column set of values.  It returns TRUE when all of the conditions are satisfied.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"all operator\">\r\nPRINT '3 LESS THAN [ALL] OF THE FIRST 8 PRIMES?' \r\nIF 3 < ALL (SELECT NUM FROM #PRIMES)\r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\n3 LESS THAN [ALL] OF THE FIRST 8 PRIMES?\r\nFALSE\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms187922.aspx\">BETWEEN<\/a> operator compares a scalar against a range of values.  It returns TRUE when the scalar is greater than or equal to the lower limit and less than or equal to the upper limit.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"between operator\">\r\nPRINT 'AVERAGE OF FIRST 8 PRIMES IS [BETWEEN] 8 AND 10' \r\nIF (SELECT AVG(NUM) FROM #PRIMES) BETWEEN 8 AND 10 \r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nAVERAGE OF FIRST 8 PRIMES IS [BETWEEN] 8 AND 10\r\nTRUE\r\n<\/pre>\n<\/p>\n<p>The logical <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188372.aspx\">AND<\/a> operator combines two BOOLEAN expressions.  It returns TRUE if both conditions are true;  otherwise, it returns false.  The example below uses simple integer expressions to demonstrate this operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"and operator\">\r\nPRINT 'TWO GREATER THAN ONE [AND] FOUR GREATER THAN THREE' \r\nIF (2 > 1) AND (4 > 3)\r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nTWO GREATER THAN ONE [AND] FOUR GREATER THAN THREE\r\nTRUE\r\n<\/pre>\n<\/p>\n<p>The logical <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188361.aspx\">OR<\/a> operator combines two BOOLEAN expressions.  It returns TRUE if any conditions are true;  otherwise, it returns false.  The example below uses simple integer expressions to demonstrate this operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"or operator\">\r\nPRINT 'TWO GREATER THAN ONE [OR] FOUR LESS THAN THREE' \r\nIF (2 > 1) OR (4 < 3)\r\n    PRINT 'TRUE' \r\nELSE\r\n    PRINT 'FALSE' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nTWO GREATER THAN ONE [OR] FOUR LESS THAN THREE\r\nTRUE\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms179859.aspx\">LIKE<\/a> operator determines if a character string matches a specific pattern.  The example below queries the sys.databases table and returns the row for the master database.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"like operator\">\r\nSELECT name as dbms_name, database_id as dbms_id \r\nFROM master.sys.databases\r\nWHERE name LIKE 'master%';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\ndbms_name  dbms_id\r\n---------- -----------\r\nmaster     1\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms177682.aspx\">IN<\/a> operator ascertains if a value matches any elements in the sub-query or list of elements.  The example below queries the sys.databases table and returns a row for each system database.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"in operator\">\r\nSELECT name as dbms_name, database_id as dbms_id \r\nFROM master.sys.databases\r\nWHERE name IN ('tempdb', 'model', 'master', 'msdb')\r\nORDER BY database_id;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\ndbms_name  dbms_id\r\n---------- -----------\r\nmaster     1\r\ntempdb     2\r\nmodel      3\r\nmsdb       4\r\n<\/pre>\n<\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms189455.aspx\">NOT<\/a> operator negates the BOOLEAN expression.  The example below queries the sys.databases table and returns the first 10 databases by id.  It removes the master database by using the NOT LIKE clause.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"not operator\">\r\nSELECT TOP 10 name as dbms_name, database_id as dbms_id \r\nFROM master.sys.databases\r\nWHERE name NOT LIKE 'master%' \r\nORDER BY database_id;\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\ndbms_name            dbms_id\r\n-------------------- -----------\r\ntempdb               2\r\nmodel                3\r\nmsdb                 4\r\nReportServer         5\r\nReportServerTempDB   6\r\nMATH                 7\r\nAdventureWorksDW2012 8\r\nAUTOS                9\r\nAdventureWorks2012   10\r\nBASIC                11\r\n<\/pre>\n<\/p>\n<p>Last but not least, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188336.aspx\">EXISTS<\/a> operator finds out if the sub-query contains at least one row.  The following example checks to see if the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa992075.aspx\">Adventure Works<\/a> database for 2012 is installed.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"exists operator\">\r\nIF EXISTS (SELECT 1 FROM master.sys.databases WHERE name = 'AdventureWorks2012')\r\n    PRINT 'TRUE - ADVENTURE WORKS IS INSTALLED' \r\nELSE\r\n    PRINT 'FALSE - ADVENTURE WORKS IS INSTALLED' ;\r\nPRINT '';\r\n<\/pre>\n<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\nTRUE - ADVENTURE WORKS IS INSTALLED\r\n<\/pre>\n<\/p>\n<p>Next time, I will be researching the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff848745.aspx\">Set Operators<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to make headway on the series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. I will exploring the Logical Operators today. In short, these operators return either a boolean TRUE or FALSE. To demonstrate many of these operators, we need a temporary table to query against. The Transaction SQL code below creates a table that contains the first eight prime numbers. Any complex real world programming example probably has&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[111,47,110,844,31,108,45,15,46,843,49,48,112,28,29],"class_list":["post-5587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-all","tag-and","tag-any","tag-between","tag-database-developer","tag-exists","tag-in","tag-john-f-miner-iii","tag-like","tag-logical-operator","tag-not","tag-or","tag-some","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5587"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5587\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5588"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}