title: T-SQL Custom Function (SQL Server 2000)
body: <p>I have a 15 digit customer number that I need to calculate a check digit for that value.
  In order to come up with the check digit I use Excel (formula is below).  </p>
  
  <p>This formula is based on something called the Luhn Algorithm (I think that is the correct spelling). 
  The 15 digit customer number is stored in a SQL Server 2000 database.  </p>
  
  <p>I would like to be able to get this calculation using a T-SQL function rather than Excel. Does anyone know how this can be done with T-SQL on a SQL Server 2000 database?</p>
  
  <p>Excel formula that will give me a check digit from a 15 digit number is this:</p>
  
  <pre><code>=MOD(SUMPRODUCT(-MID(TEXT(MID(A2,ROW(INDIRECT("1:"&amp;LEN(A2))),1)*(MOD(ROW(INDIRECT("1:"&amp;LEN(A2)))+LEN(A2)+1,2)+1),"00"),{1,2},1)),10)
  </code></pre>