身份证校验
-
SQL开发知识:SqlServer2000+ 身份证合法校验函数的示例代码
``sql,CREATE FUNCTION dbo.IsValidIDCard(@IDCard NVARCHAR(18)),RETURNS BIT,AS,BEGIN, DECLARE @Result BIT, @CheckCode INT, @Weights TABLE (Weight INT),, INSERT INTO @Weights VALUES (7), (9), (10), (5), (8), (4), (2), (1), (6), (3), (7), (9), (10), (5), (8), (4), (2),, IF LEN(@IDCard) != 18, RETURN 0,, IF NOT @IDCard REGEXP '^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$', RETURN 0,, SET @CheckCode = (SELECT SUM(CAST(SUBSTRING(@IDCard, i, 1) AS INT) * Weight FROM @Weights JOIN (VALUES (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18), (19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36)) AS x(i, Weight) ON x.i ˂= 17) / 11) % 11,, IF @CheckCode = CAST(SUBSTRING(@IDCard, 18, 1) AS INT), SET @Result = 1, ELSE, SET @Result = 0,, RETURN @Result,END,``