IP 蠍磯蓋朱 "255.255.255.255" 螳 企. 255朱 蟆 1byte襦 豕 企. 襷豺(".")襯 誤覃 4byte襦 願屋 . 蠏碁 朱 覯 襦蠏碁襾碁れ 企 覓語 IP襯 int朱 覲蟆渚 螻 . 讌襷 SQL Server 蟆曙 int 4byte 蠍磯ゼ 螳讌讌襷 unsigned螳 signed int企襦 "255.255.255.255"襯 覈 . 蠏碁り 8byte bigint襯 蟆 蟾. 蠏碁覩襦 れ螻 螳 binary(4)襦 蟯襴覃 . 企蟆 蟯襴覃 IP 覯一磯 朱襦 焔リ骸 蟯襴朱 2襷襴 朱ゼ ′ . IP れ螻 螳 覲蟆渚覃 .
declare @ip varchar(15)
set @ip = '121.153.53.57'
--121.168.96.235
select
ip_binary
, convert(varchar, convert(smallint, substring(ip_binary, 1, 1))) + '.' +
convert(varchar, convert(smallint, substring(ip_binary, 2, 1))) + '.' +
convert(varchar, convert(smallint, substring(ip_binary, 3, 1))) + '.' +
convert(varchar, convert(smallint, substring(ip_binary, 4, 1))) ip_string
from(
select
CONVERT(binary(1), CAST(PARSENAME(@ip, 4) AS tinyint)) +
CONVERT(binary(1), CAST(PARSENAME(@ip, 3) AS tinyint)) +
CONVERT(binary(1), CAST(PARSENAME(@ip, 2) AS tinyint)) +
CONVERT(binary(1), CAST(PARSENAME(@ip, 1) AS tinyint)) ip_binary
) t