我正在尝试在SQL Server上运行此脚本:
CREATE FUNCTION [dbo].[IPAddressToCidr](@IP AS VARCHAR(15))
RETURNS INT
AS
BEGIN
DECLARE @result INT;
DECLARE @ipInt BIGINT;
SET @ipInt = CONVERT(BIGINT, PARSENAME(@IP, 1)) + CONVERT(BIGINT, PARSENAME(@IP, 2)) * 256 + CONVERT(BIGINT, PARSENAME(@IP, 3)) * 65536 + CONVERT(BIGINT, PARSENAME(@IP, 4)) * 16777216;
SET @result = CAST(( 32 - LOG(4294967296 - @ipInt, 2)) AS INT);
RETURN @result;
END;
结果错误是:
Msg 174,Level 15,State 1,Procedure IPAddressToCidr,Line 11
log函数需要1个参数。
我正在努力......
SELECT LOG(4294967296,2) /*this has problem*/
SELECT LOG(4294967296) /*this one works but with different results */
Log()
函数有两个参数的帮助吗?
答案 0 :(得分:4)
SQL Server 2012+
支持LOG(expr, [base])
使用较低版本,您可以使用简单的Math来获得结果:
SELECT LOG(1024)/LOG(2) -- 10, 2 ^ 10 = 1024
SELECT LOG(1000)/LOG(10) -- 3, 10 ^ 3 = 1000
的 LiveDemo
强>
<强> Change of base formula
强>
LOG(@arg,base) = LOG(@arg) / LOG(base)