{"id":5530,"date":"2013-05-04T00:00:31","date_gmt":"2013-05-04T00:00:31","guid":{"rendered":"http:\/\/craftydba.com\/?p=5530"},"modified":"2016-04-23T01:13:36","modified_gmt":"2016-04-23T01:13:36","slug":"comparison-operators-part-1","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5530","title":{"rendered":"Comparison Operators &#8211; Part 1"},"content":{"rendered":"<p>I am going to keep the ball rolling with my 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\/ms188074.aspx\">Comparison Operators<\/a> today.  <\/p>\n<p>These comparison operators are known as the less than <span style=\"color: #DD0000;\"><<\/span>, less than or equal to <span style=\"color: #DD0000;\"><=<\/span>, equal to <span style=\"color: #DD0000;\">=<\/span>, greater than <span style=\"color: #DD0000;\">><\/span>, and greater than or equal to <span style=\"color: #DD0000;\">>=<\/span> symbols in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Inequality_(mathematics)\">inequality<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Equality_(mathematics)#Logical_formulations\">equality<\/a> mathematics. <\/p>\n<p>There are both ANSI standard and Microsoft specific comparison operators.  I will be using the tiny integer data type to declare variables in the examples below.  This allows us to focus just on the comparison logic instead of worrying about data types and implicit data type conversions.  <\/p>\n<p>Each example below shows both a positive and negative test using each ANSI standard operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"ansi standard - comparision operators\">\r\n--\r\n--  Comparision operators - ANSI standard\r\n--\r\n\r\n-- Declare variables\r\ndeclare @one tinyint = 1;\r\ndeclare @two tinyint = 2;\r\ndeclare @three tinyint = 3;\r\n\r\n-- Ansi operator - <\r\nif @one < @two print 'A01 - one is less than two';\r\nif not (@three < @two) print 'A02 - three is not less than two';\r\nprint ' ';\r\n\r\n-- Ansi operator - >\r\nif @two > @one print 'A03 - two is greater than one';\r\nif not (@two > @three) print 'A04 - two is not less than three';\r\nprint ' ';\r\n\r\n-- Ansi operator - =\r\nif @two = @two print 'A05 - two equals two';\r\nif not (@two = @three) print 'A06 - two not equal to three';\r\nprint ' ';\r\n\r\n-- Ansi operator - <>\r\nif not (@two <> @two) print 'A07 - false - two not equal to two';\r\nif @two <> @three print 'A08 - true - two not equal to three';\r\nprint ' ';\r\n\r\n-- Ansi combo operator - <= \r\nif @one <= @two print 'A09 - one is less than or equal to two';\r\nif @two <= @two print 'A10 - two is less than or equal to two';\r\nprint ' ';\r\n\r\n-- Ansi combo operator - >= \r\nif @two >= @two print 'A11 - two is greater than or equal to two';\r\nif @three >= @two print 'A12 - three is greater than or equal to two';\r\nprint ' ';\r\n<\/pre>\n<\/p>\n<p>The output of each comparison is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nA01 - one is less than two\r\nA02 - three is not less than two\r\n \r\nA03 - two is greater than one\r\nA04 - two is not less than three\r\n \r\nA05 - two equals two\r\nA06 - two not equal to three\r\n \r\nA07 - false - two not equal to two\r\nA08 - true - two not equal to three\r\n \r\nA09 - one is less than or equal to two\r\nA10 - two is less than or equal to two\r\n \r\nA11 - two is greater than or equal to two\r\nA12 - three is greater than or equal to two \r\n<\/pre>\n<\/p>\n<p>The TSQL examples below shows one correct use of each Microsoft specific comparison operator.  <\/p>\n<p><span style=\"color: #DD0000;\">I suggest not using them in coding since your code will not be portable between different database engines.<\/span><\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"microsoft specific - comparision operators\">\r\n--\r\n--  Comparision operators - Microsoft specific\r\n--\r\n\r\n-- Declare variables\r\ndeclare @four tinyint = 4;\r\ndeclare @five tinyint = 5;\r\n\r\n-- Microsoft operator - !=\r\nif @four != @five print 'M01 - four is not equal to five';\r\n\r\n-- Microsoft operator - !>\r\nif @four !> @five print 'M02 - four is not greater than five';\r\n\r\n-- Microsoft operator - !<\r\nif @five !< @four print 'M03 - five is not less than four';\r\n<\/pre>\n<\/p>\n<p>The output of each comparison is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nM01 - four is not equal to five\r\nM02 - four is not greater than five\r\nM03 - five is not less than four\r\n\r\n<\/pre>\n<\/p>\n<p>Next time, I will be using comparison operators with different data types.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to keep the ball rolling with my 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 Comparison Operators today. These comparison operators are known as the less than = symbols in inequality and equality mathematics. There are both ANSI standard and Microsoft specific comparison operators. I will be using the tiny integer data type to declare variables in the examples below. This allows us to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[841,31,15,28,29],"class_list":["post-5530","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-comparision-operator","tag-database-developer","tag-john-f-miner-iii","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5530","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=5530"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5530\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5459"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}