{"id":5553,"date":"2013-05-05T00:00:29","date_gmt":"2013-05-05T00:00:29","guid":{"rendered":"http:\/\/craftydba.com\/?p=5553"},"modified":"2016-04-23T01:10:48","modified_gmt":"2016-04-23T01:10:48","slug":"comparison-operators-part-2","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5553","title":{"rendered":"Comparison Operators &#8211; Part 2"},"content":{"rendered":"<p>I am going pick up writing 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 continue examining 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>I will be examining how a sample operator works with character, date\/time, money and binary data types.  Remember, character data types use the ASCII or UNICODE chart to determine if one character is greater than another.  All other data types are based upon the value stored on disk or in memory.<\/p>\n<p>The first example below shows the less than operator used with character data type.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"character data - comparision operators\">\r\n\r\n--\r\n--  Comparision operators - Character data type\r\n--\r\n\r\n-- Declare variables\r\ndeclare @chr_one varchar(2) = 'AB';\r\ndeclare @chr_two varchar(2) = 'ab';\r\n\r\n-- Both variables are the same\r\nprint @chr_one;\r\nprint @chr_two;\r\nprint ' ';\r\n\r\n-- Ansi operator - <\r\nif @chr_one < @chr_two \r\n    print 'CHAR - Upper is less than lower.';\r\nelse\r\n    print 'CHAR - Lower is less than upper.';\r\nprint ' ';\r\n<\/pre>\n<\/p>\n<p>The output of the first example is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nAB\r\nab\r\n \r\nCHAR - Lower is less than upper.\r\n<\/pre>\n<\/p>\n<p>The second example below shows the equal operator used with date\/time data type.  In this example, both variables are equal due to precision loss during rounding.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"date\/time data - comparision operators\">\r\n\r\n--\r\n--  Comparision operators - Date\/Time data type\r\n--\r\n\r\n-- Declare variables\r\ndeclare @dt_one smalldatetime = '2013-05-01 23:59:59.999';\r\ndeclare @dt_two smalldatetime = '2013-05-02 00:00:00.000'\r\n\r\n-- Both variables are the same (rounding)\r\nprint @dt_one;\r\nprint @dt_two;\r\nprint ' ';\r\n\r\n-- Ansi operator - =\r\nif @dt_two = @dt_one\r\n    print 'DATE \/ TIME - Both variables are equal.';\r\nelse\r\n    print 'DATE \/ TIME - Both variables are not equal.';\r\nprint ' ';\r\n<\/pre>\n<\/p>\n<p>The output of the second example is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nMay  2 2013 12:00AM\r\nMay  2 2013 12:00AM\r\n \r\nDATE \/ TIME - Both variables are equal.\r\n<\/pre>\n<\/p>\n<p>The third example below shows the greater than operator used with money data type.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"numeric data - comparision operators\">\r\n--\r\n--  Comparision operators - Money data type\r\n--\r\n\r\n-- Declare variables\r\ndeclare @num_one smallmoney = 321.22;\r\ndeclare @num_two smallmoney = 312.21;\r\n\r\n-- Both variables are not the same\r\nprint @num_one;\r\nprint @num_two;\r\nprint ' ';\r\n\r\n-- Ansi operator - >\r\nif @num_one > @num_two\r\n    print 'MONEY - Variable one is greater than two.';\r\nelse\r\n    print 'MONEY - Variable one is not greater than two.';\r\nprint ' ';\r\n<\/pre>\n<\/p>\n<p>The output of the third example is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n321.22\r\n312.21\r\n \r\nMONEY - Variable one is greater than two.\r\n<\/pre>\n<\/p>\n<p>The fourth example below shows the not equal to operator used with binary data type.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"binary data - comparision operators\">\r\n\r\n--\r\n--  Comparision operators - Binary data type\r\n--\r\n\r\n-- Declare variables\r\ndeclare @bin_one binary(1) = 0x3E;\r\ndeclare @bin_two binary(1) = 0xE3;\r\n\r\n-- Both variables are not the same\r\nprint @bin_one;\r\nprint @bin_two;\r\nprint ' ';\r\n\r\n-- Ansi operator - <>\r\nif @bin_one <> @bin_two\r\n    print 'BINARY - Variable one is not equal to two.';\r\nelse\r\n    print 'BINARY - Variable one is equal to two.';\r\nprint ' ';\r\n<\/pre>\n<\/p>\n<p>The output of the fourth example is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n0x3E\r\n0xE3\r\n \r\nBINARY - Variable one is not equal to two.\r\n<\/pre>\n<\/p>\n<p>Next time, I will be chatting about how to use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc645922.aspx\">Compound Operators<\/a> when writing Transaction SQL scripts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going pick up writing 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 continue examining the Comparison Operators today. These comparison operators are known as the less than = symbols in inequality and equality mathematics. I will be examining how a sample operator works with character, date\/time, money and binary data types. Remember, character data types use the ASCII or UNICODE chart to determine if one character&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-5553","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\/5553","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=5553"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5553\/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=5553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}