--CELKO--
3/30/2007 5:55:00 PM
>> This works as a where clause in a query, but I'm trying to change it into a UDF to find valid social security numbers through SQL. m<<
That is a mistake. Make it into a CHECK() constraint and assure that
you have clean data, formatted in one and only one way in the
schema. And your code will be portable, too!
You are depending on *every* application -- present and future -- to
do the work for you and that nobody will come into the DB via QA or
other tools. Too much risk.
Next what you posted does not really work. The SSN is divided as
follows: the area number (first three digits), group number (fourth
and fifth digits), and serial number (last four digits).
To determine if an SSN is invalid consider the following: No SSNs with
an area number in the 800 or 900 series, or 000 area number, have been
assigned. No SSNs with an area number above 772 have been assigned in
the 700 series.
No SSN's with a "00" group number or "0000" serial number have been
assigned. No SSNs with an area number of "666" have been or will be
assigned (Really -- Mark of the beast and all that crap!). Information
about the SSN and SSNs that have been assigned is available at "High
Group List and Other Ways to Determine if an SSN is Valid" on their
website.
It is hard to code and it needs to be updated at least once a year as
new numbers are issued and people die to be 100% correct. Google
around for open source validators.
Aaron's code has an additional problem. Are you going to use the
proprietary BIT as a Boolean flag in applications? Does your app
language use +1 or -1 for TRUE? Or is 0 used for TRUE? The answer is
"yes" for all three situations.
I clean up this kind of stuff for a living. It is very hard work, not
a few lines of UDF code or even a simple CHECK() constraint.