{"id":6279,"date":"2013-09-04T00:00:23","date_gmt":"2013-09-04T00:00:23","guid":{"rendered":"http:\/\/craftydba.com\/?p=6279"},"modified":"2016-04-21T21:09:43","modified_gmt":"2016-04-21T21:09:43","slug":"math-functions-round","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=6279","title":{"rendered":"Math Functions &#8211; ROUND()"},"content":{"rendered":"<p>I am going to keep going with my series of short articles or tidbits on Transaction SQL <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms177516.aspx\">math functions<\/a>.  Most, if not all, of these functions have been in the product before the release of SQL Server 2005.  <\/p>\n<p>I am very proud of my daughter making the advance math class in sixth grade.  I am dedicating these blogs to her hard class work and love of the subject.<\/p>\n<p>I will be exploring the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ms175003.aspx\">ROUND<\/a>() function today.  This function takes as input a expression in either exact numeric or approximate numeric form.  This does not include the BIT data type.  It returns a numeric value rounded to the specified length or precision.<\/p>\n<p>The example below tests the ROUND() function with three different inputs:  negative numbers, positive numbers and a null value.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"round() - math functions - positive test case\">\r\n--\r\n--  The ROUND Function\r\n--\r\n\r\n-- Round number to correct length\r\nSELECT \r\n    ROUND(10.45, 1) AS TENTHS, \r\n    ROUND(121.45, -1) AS NEAREST10, \r\n    ROUND(NULL, -1) AS UNKNOWN;\r\nGO\t\r\n<\/pre>\n<\/p>\n<p>The output from the test is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nTENTHS      NEAREST10    UNKNOWN\r\n----------- -----------  -----------\r\n10.50       120.00       NULL\r\n<\/pre>\n<\/p>\n<p>This function returns some interesting results if the maximum length is passed.  We end up with return value of either a 0 or 1000 depending on how close the number xxx.xx is to the mid point.  Since 1000 can no be stored in decimal(5,2) data type, a arithmetic overflow is generated.  See the below example.  <\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"round() - math functions - negative test case\">\r\n--\r\n--  Incorrect length\r\n--\r\n\r\n-- Returns zero\r\nSELECT ROUND(499.99, -3) AS ZERO;\r\n\r\n-- Errors with overflow\r\nSELECT ROUND(500.00, -3) AS OVERFLOW;\r\n<\/span><\/pre>\n<\/p>\n<p>The output from the example is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\noutput: \r\n\r\nZERO   \r\n---------\r\n0.00   \r\n\r\nOVERFLOW\r\n---------\r\nMsg 8115, Level 16, State 2, Line 1\r\nArithmetic overflow error converting expression to data type numeric.\r\n<\/pre>\n<\/p>\n<p>The overflow be avoided by casting the number to a real before rounding!<\/p>\n<p>To recap, this function will return the numeric expression rounded to the specified length or precision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to keep going with my series of short articles or tidbits on Transaction SQL math functions. Most, if not all, of these functions have been in the product before the release of SQL Server 2005. I am very proud of my daughter making the advance math class in sixth grade. I am dedicating these blogs to her hard class work and love of the subject. I will be exploring the ROUND() function today. This function takes as input a expression in either exact numeric or approximate numeric&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5430,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[31,15,913,916,29],"class_list":["post-6279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-very-short-articles","tag-database-developer","tag-john-f-miner-iii","tag-math-function","tag-round","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6279","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=6279"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/6279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/media\/5430"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}