Use the following code to validate Social Security Number (USA). The rules used for validation are based on SSA website, Wikipedia and HowStuffWorks.com. There may be more rules to the validation. If you find any, keep me posted! 'njoy Validating!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ankeet
-- Create date: 6/19/2008
-- Description: Validates SSN
-- =============================================
ALTER FUNCTION ValidateSSN
(
@SSN varchar(11)
)
RETURNS varchar(255)
AS
BEGIN
/*
USAGE: SELECT DBO.ValidateSSN(777992322)
BASED ON:
http://people.howstuffworks.com/social-security-number2.htm
http://en.wikipedia.org/wiki/Social_Security_number
http://www.socialsecurity.gov/employer/stateweb.htm
http://en.wikipedia.org/wiki/Individual_Taxpayer_Identification_Number
The Administration publishes the last group number used for each area number.[17].
Since group numbers are allocated in a regular (if unusual) pattern, it is possible
to identify an unissued SSN that contains an invalid group number. Despite these
measures, many fraudulent SSNs cannot easily be detected using only publicly available
information.
*/
DECLARE @RETURN VARCHAR(255)
DECLARE @VALID TINYINT
DECLARE @tSSN VARCHAR(9)
SET @SSN = REPLACE(@SSN, '-', '')
SET @VALID = 1
SET @RETURN = ''
DECLARE @i INT
SET @i = 1
WHILE @i <= LEN(@SSN)
BEGIN
IF ASCII(SUBSTRING(@SSN, @i, 1)) NOT BETWEEN 48 AND 57
BEGIN
SELECT @RETURN = @RETURN + 'NO; Non-Numeric Characters Not Allowed/n', @VALID = 0
GOTO FINALLY_RETURN
END
SET @i = @i + 1
END
IF LEN(@SSN) > 9
SELECT @RETURN = @RETURN + 'NO; Maximum 9 Numbers Allowed/n', @VALID = 0
IF (CONVERT(SMALLINT, LEFT(@SSN, 3)) > 772 OR CONVERT(SMALLINT, LEFT(@SSN, 3)) = 666)
AND (LEFT(@SSN, 1) <> 9 AND SUBSTRING(@SSN, 4, 1) <> 7 AND SUBSTRING(@SSN, 4, 1) <> 8)
SELECT @RETURN = @RETURN + 'NO; Area Number (left 3) Not Valid/n', @VALID = 0
IF CONVERT(INT, LEFT(@SSN,9)) BETWEEN 987654320 AND 987654329
SELECT @RETURN = @RETURN + 'NO; Number Reserved For Advertisement (987-65-4320 to 987-65-4329)/n', @VALID = 0
IF LEFT(@SSN, 3) = '000' OR RIGHT(@SSN, 4) = '0000' OR SUBSTRING(@SSN, 4, 2) = '00'
SELECT @RETURN = @RETURN + 'NO; Consecutive Zeroes Not Allowed/n', @VALID = 0
IF @VALID = 1 SET @RETURN = 'YES'
FINALLY_RETURN:
RETURN @RETURN
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Ankeet
-- Create date: 6/19/2008
-- Description: Validates SSN
-- =============================================
ALTER FUNCTION ValidateSSN
(
@SSN varchar(11)
)
RETURNS varchar(255)
AS
BEGIN
/*
USAGE: SELECT DBO.ValidateSSN(777992322)
BASED ON:
http://people.howstuffworks.com/social-security-number2.htm
http://en.wikipedia.org/wiki/Social_Security_number
http://www.socialsecurity.gov/employer/stateweb.htm
http://en.wikipedia.org/wiki/Individual_Taxpayer_Identification_Number
The Administration publishes the last group number used for each area number.[17].
Since group numbers are allocated in a regular (if unusual) pattern, it is possible
to identify an unissued SSN that contains an invalid group number. Despite these
measures, many fraudulent SSNs cannot easily be detected using only publicly available
information.
*/
DECLARE @RETURN VARCHAR(255)
DECLARE @VALID TINYINT
DECLARE @tSSN VARCHAR(9)
SET @SSN = REPLACE(@SSN, '-', '')
SET @VALID = 1
SET @RETURN = ''
DECLARE @i INT
SET @i = 1
WHILE @i <= LEN(@SSN)
BEGIN
IF ASCII(SUBSTRING(@SSN, @i, 1)) NOT BETWEEN 48 AND 57
BEGIN
SELECT @RETURN = @RETURN + 'NO; Non-Numeric Characters Not Allowed/n', @VALID = 0
GOTO FINALLY_RETURN
END
SET @i = @i + 1
END
IF LEN(@SSN) > 9
SELECT @RETURN = @RETURN + 'NO; Maximum 9 Numbers Allowed/n', @VALID = 0
IF (CONVERT(SMALLINT, LEFT(@SSN, 3)) > 772 OR CONVERT(SMALLINT, LEFT(@SSN, 3)) = 666)
AND (LEFT(@SSN, 1) <> 9 AND SUBSTRING(@SSN, 4, 1) <> 7 AND SUBSTRING(@SSN, 4, 1) <> 8)
SELECT @RETURN = @RETURN + 'NO; Area Number (left 3) Not Valid/n', @VALID = 0
IF CONVERT(INT, LEFT(@SSN,9)) BETWEEN 987654320 AND 987654329
SELECT @RETURN = @RETURN + 'NO; Number Reserved For Advertisement (987-65-4320 to 987-65-4329)/n', @VALID = 0
IF LEFT(@SSN, 3) = '000' OR RIGHT(@SSN, 4) = '0000' OR SUBSTRING(@SSN, 4, 2) = '00'
SELECT @RETURN = @RETURN + 'NO; Consecutive Zeroes Not Allowed/n', @VALID = 0
IF @VALID = 1 SET @RETURN = 'YES'
FINALLY_RETURN:
RETURN @RETURN
END
GO
Comments