I have a database field of Brazilian CPF numbers and want to check for their validity. These are 11 digit strings which are 9 digits and 2 checksum digits.
I currently implemented the checksum in MS Excel (see below) but I'd like to figure out a way to do it in SQL.
Checksum works as follows: (Hold on tight, this is nuts.)
- The CPF number is written in the form ABCDEFGHI / JK or directly as ABCDEFGHIJK, where the digits can not all be the same as each other.
- The J is called 1st digit check of the CPF number.
- The K is called the 2nd check digit of the CPF number.
First digit (J):
Multiply each digit of the first 9 by a constant:
10*A + 9*B + 8*C + 7*D + 6*E + 5*F + 4*G + 3*H + 2*IDivide this sum by 11 and if the remainder is 0 or 1, J will be 0. If the remainder is >=2, J will be
11 - remainder.
Second digit (K): (Same calculation but including digit J)
Multiply each digit of the first 10 by a constant:
11A + 10B + 9C + 8D + 7E + 6F + 5G + 4H + 3I + 2JDivide this sum by 11 and if the remainder is 0 or 1, K will be 0. If the remainder is >=2, K will be
11 - remainder.
--Implementation in MS Excel--
Assuming the CPF is in A2.
Optimizations here are welcome but not really the point of this question.
Digit J: =IF(MOD(SUM(MID($A2,1,1)*10,MID($A2,2,1)*9,MID($A2,3,1)*8,MID($A2,4,1)*7,MID($A2,5,1)*6,MID($A2,6,1)*5,MID($A2,7,1)*4,MID($A2,8,1)*3,MID($A2,9,1)*2),11)<=1,NUMBERVALUE(LEFT(RIGHT($A2,2),1))=0,NUMBERVALUE(LEFT(RIGHT($A2,2),1))=(11-MOD(SUM(MID($A2,1,1)*10,MID($A2,2,1)*9,MID($A2,3,1)*8,MID($A2,4,1)*7,MID($A2,5,1)*6,MID($A2,6,1)*5,MID($A2,7,1)*4,MID($A2,8,1)*3,MID($A2,9,1)*2),11)))
Digit K:
=IF(MOD(SUM(MID($A2,1,1)*11,MID($A2,2,1)*10,MID($A2,3,1)*9,MID($A2,4,1)*8,MID($A2,5,1)*7,MID($A2,6,1)*6,MID($A2,7,1)*5,MID($A2,8,1)*4,MID($A2,9,1)*3,MID($A2,10,1)*2),11)<=1,NUMBERVALUE(LEFT(RIGHT($A2,1),1))=0,NUMBERVALUE(LEFT(RIGHT($A2,1),1))=(11-MOD(SUM(MID($A2,1,1)*11,MID($A2,2,1)*10,MID($A2,3,1)*9,MID($A2,4,1)*8,MID($A2,5,1)*7,MID($A2,6,1)*6,MID($A2,7,1)*5,MID($A2,8,1)*4,MID($A2,9,1)*3,MID($A2,10,1)*2),11)))