SQL Server : update table Convert Int To IP String -


i have problem example table1

name                     ip                         os        version ---------------------------------------------------------------------- jonh        0x20010db80a0b12f00000000000000001      os         8.184 bob         0x20010db80a0b12f00000000000000092      win        10.844 alice       0x20010db80a0b12f00000000000000051     linux       ubuntu 

and need convert or update ip column (varbinary) string. try update whole table this:

declare @ip int   set @ip =  *   select *  table1,ip  update [dbo].[table1] set ip = convert(varchar(3), (@ip/16777216) & 255) + '.'          + convert(varchar(3), (@ip/65536) & 255) + '.'          + convert(varchar(3), (@ip/256) & 255) + '.'          + convert(varchar(3), @ip & 255) ip 

but not work. type of declaration work 1 row/ip not update whole table.

my original table has on 1000 rows.

if logic conversion correct please try code below

  /*  create table table1 ([name] varchar(100), [ip] varchar(100),[os] varchar(100),[version] varchar(100)); insert table1 values ('jonh',        '0x20010db80a0b12f00000000000000001'      ,'os',         '8.184') ,('bob',         '0x20010db80a0b12f00000000000000092'      ,'win',        '10.844') ,('alice',    '0x20010db80a0b12f00000000000000051'     ,'linux',       'ubuntu');*/  select     name,    ip=      convert(varchar(3), (cast(ip varbinary(100))/16777216) & 255) + '.'      + convert(varchar(3), (cast(ip varbinary(100))/65536) & 255) + '.'      + convert(varchar(3), (cast(ip varbinary(100))/256) & 255) + '.'      + convert(varchar(3), cast(ip varbinary(100)) & 255),   os,   version  table1 

see demo link


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -