title: Convert signed int to string ip address in SQL Server
body: <p>I'm retrieving a signed int from a SQL Server database and need to convert it to a "normal" looking dotted string for display to users.</p>
  
  <p>Googling, I found this code:</p>
  
  <pre><code>SELECT 
     dbo.IPADDRESS.IPADDRESS,
     CAST(ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 16777216 ), 0, 1) AS varchar(4)) + '.' +
     CAST((ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 65536 ), 0, 1) % 256) AS varchar(4)) + '.' +
     CAST((ROUND( (cast(dbo.IPADDRESS.IPADDRESS as bigint) / 256 ), 0, 1) % 256) AS varchar(4)) + '.' + 
     CAST((cast(dbo.IPADDRESS.IPADDRESS as bigint) % 256 ) AS varchar(4)) as IPDottedNotation
  FROM  
     dbo.IPADDRESS
  </code></pre>
  
  <p>which works some of the time, but produces wacky output other times.  For example, converting this <code>-1951276725</code> yields the result <code>-116.-78.-30.-181</code>.</p>
  
  <p>Any suggestions?  Thanks.</p>