--数字转IP地址函数:
IF OBJECT_ID('dbo.fn_IP2Str')>0
DROP FUNCTION dbo.fn_IP2Str
GO
CREATE FUNCTION [dbo].[fn_IP2Str] (
@InIP BIGINT
)
RETURNS NVARCHAR(15)
AS
BEGIN
IF @InIP IS NULL
RETURN '0.0.0.0'
DECLARE @ip BIGINT
SET @ip = @InIP
SET @ip = @ip + 0x100000000
RETURN
CAST(((@ip & 0xff000000) / 0x1000000) AS NVARCHAR(3)) + '.' +
CAST(((@ip & 0xff0000) / 0x10000) AS NVARCHAR(3)) + '.' +
CAST(((@ip & 0xff00) / 0x100) AS NVARCHAR(3)) + '.' +
CAST((@ip & 0xff) AS NVARCHAR(3))
END
GO

--调用示例:
SELECT dbo.fn_IP2Str(4294967295)


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
IF OBJECT_ID('dbo.f_Int2IP')>0
DROP FUNCTION dbo.f_Int2IP
CREATE FUNCTION f_Int2IP
(
@IP BIGINT
)
RETURNS VARCHAR(15)
AS
BEGIN
DECLARE @re VARCHAR(16)
SET @re = ''
SELECT @re = @re+'.'+CAST(@IP/ID AS VARCHAR), @IP = @IP%ID
FROM (
SELECT ID = CAST(16777216 AS BIGINT)
UNION ALL SELECT 65536
UNION ALL SELECT 256
UNION ALL SELECT 1
)a
RETURN(STUFF(@re, 1, 1, ''))
END
GO
--调用示例:
select dbo.f_Int2IP(333444343)