<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; unsigned</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/unsigned/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unsigned Integer Arithmetic in SQL</title>
		<link>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</link>
		<comments>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794#comments</comments>
		<pubDate>Sat, 12 Jun 2010 16:45:27 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[integers]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</guid>
		<description><![CDATA[Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types can be positive and negative with the exception of <a href="http://msdn.microsoft.com/en-us/library/ms187745.aspx" target="_blank">TINYINT</a>. MySQL implements the concept of <a href="http://en.wikipedia.org/wiki/Signedness" target="_blank">signedness</a> so we can specify that TINYINT ranges from –128 to 127 or 0 to 255.</p>
<p>What would you expect SQL Server to do with this?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7944"><td class="code" id="p794code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @num1 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">50</span>, @num2 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">75</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @num1 <span style="color: #808080;">-</span> @num2;</pre></td></tr></table></div>

<p>Well it errors. Good RDBMS! </p>
<pre>Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type tinyint.</pre>
<p>What does MySQL do? Lets see.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7945"><td class="code" id="p794code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	num2 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1<span style="color: #000033;">,</span>
	num2
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008080;">50</span><span style="color: #000033;">,</span>
	<span style="color: #008080;">75</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mysql unsigned division" border="0" alt="mysql unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division_thumb.png" width="644" height="216" /></a> </p>
<p>Oh dear not a pretty behaviour! Bad RDBMS! A colleague said to me “you need to set <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" target="_blank">sql_mode</a> to traditional”. Mmmmmm, I thought I was running in traditional mode already. As it turns out we need to set the NO_UNSIGNED_SUBTRACTION option.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p794code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7946"><td class="code" id="p794code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> @@<span style="color: #990099; font-weight: bold;">SESSION</span>.sql_mode <span style="color: #CC0099;">=</span> <span style="color: #008000;">'TRADITIONAL,NO<span style="color: #008080; font-weight: bold;">_</span>UNSIGNED<span style="color: #008080; font-weight: bold;">_</span>SUBTRACTION'</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="mysql correct unsigned division" border="0" alt="mysql correct unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division_thumb.png" width="644" height="219" /></a> </p>
<p>Got to love MySQL for keeping you on your toes!</p>
<p><map name='google_ad_map_794_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/794?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_794_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=794&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Funsigned-integer-arithmetic-in-sql%2F794' title="Unsigned Integer Arithmetic in SQL" alt=" Unsigned Integer Arithmetic in SQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

