sponsored links

sqlserver中int类型的ip地址转字符串的代码

sqlserver中int类型的ip地址转字符串的代码:

有群友问到,就试着做了一下,还挺麻烦的。所以记录下来作为备忘。

declare @a int;
select @a = -1062729436;
if @a < 0
select '' + cast(((@a & 0xFF000000) /16777216 + 256) as varchar) + '.' + cast(((@a & 0x00FF0000) / 65536) as varchar) + '.' + cast(((@a & 0x0000FF00) / 256) as varchar) + '.' + cast((@a & 0x000000FF) as varchar)
else
select '' + cast(((@a & 0xFF000000) /16777216) as varchar) + '.' + cast(((@a & 0x00FF0000) / 65536) as varchar) + '.' + cast(((@a & 0x0000FF00) / 256) as varchar) + '.' + cast((@a & 0x000000FF) as varchar)

Tags: