Click here to Skip to main content
11,636,300 members (78,965 online)
Click here to Skip to main content

Tagged as

SQL function to verify 'Check Digit' of given VISA/Master Card

, 23 Oct 2010 CPOL 12K 12
Rate this:
Please Sign up or sign in to vote.
It help's to verify if the card number provided by the user is valid or not.
Hi,
 
Normally, every Visa/Master card have the 16 digits number printed(embossed) on the card. Last 16th digit is called as check digit.
 
There is an algorithm called MOD10 to verify that card number is valid or not. Here I am providing a link for detailed description about check digits logic: Cards Check Digits[^]
 
I am providing a SQL Server function for 'Check Digit' for Visa/Master Cards(16 Digits).
-- About using function: Pass the first 15 digits of the card no to the function 
-- and function will retuns the last check digit. 
-- That returned value should match with user provided 16th digit.

CREATE FUNCTION [DBO].[MOD10CHECKDIGIT](@STR VARCHAR(15))
RETURNS VARCHAR(1)
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @RETURNSTR VARCHAR(100);
DECLARE @REVERSESTR VARCHAR(15);
DECLARE @CNT INT;
DECLARE @SUMVAL INT;
DECLARE @TEMPVAL INT;
DECLARE @TEMPVAL1 INT;
IF(LEN(@STR)<>15)
   SET @RETURNSTR='NO';
ELSE
  BEGIN
    SET @REVERSESTR = REVERSE(@STR);
    SET @SUMVAL=0;
    SET @CNT=1;
     WHILE @CNT < 16
      BEGIN
       SET @TEMPVAL =0;
       SET @TEMPVAL1 =0;
       IF((@CNT%2)<>0)
           SET @TEMPVAL =  (CONVERT(INT,SUBSTRING(@REVERSESTR,@CNT,1) * 2));
       ELSE
           SET @TEMPVAL =  (CONVERT(INT,SUBSTRING(@REVERSESTR,@CNT,1)));
       IF(@TEMPVAL>9)
          BEGIN
                SET @TEMPVAL1 = CONVERT(INT,SUBSTRING(CONVERT(VARCHAR,@TEMPVAL),1,1)) +
                             CONVERT(INT,SUBSTRING(CONVERT(VARCHAR,@TEMPVAL),2,1));
                SET @TEMPVAL=@TEMPVAL1;
          END;
       SET @SUMVAL = @SUMVAL + @TEMPVAL;
       SET @CNT= @CNT + 1;
      END
     SET @RETURNSTR=CONVERT(VARCHAR,(10 - (@SUMVAL%10)));
     --SET @RETURNSTR=CONVERT(VARCHAR,@SUMVAL);
  END
RETURN @RETURNSTR;
END
 
Note: Every VISA card starts with digit "4" and Mastercard stats with "5".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajesh Anuhya
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 3 Interesting exercise in T-SQL progra... Pin
Rod Kemp23-Oct-10 3:49
memberRod Kemp23-Oct-10 3:49 
GeneralThanks Sandeep.., i want some more help from u, to edit my a... Pin
Rajesh Puli23-Oct-10 1:17
memberRajesh Puli23-Oct-10 1:17 
GeneralReason for my vote of 5 Ok..i Have to admit..This is great. ... Pin
GPUToaster23-Oct-10 1:16
memberGPUToaster23-Oct-10 1:16 
GeneralEdited, Formatted and published! Pin
Sandeep Mewara23-Oct-10 0:55
mvpSandeep Mewara23-Oct-10 0:55 
GeneralOk i will update in tip/trick Pin
Rajesh Puli22-Oct-10 23:01
memberRajesh Puli22-Oct-10 23:01 
GeneralOk. Can you please add what is 'Check Digit(mod10)'? Can you... Pin
Sandeep Mewara22-Oct-10 22:35
mvpSandeep Mewara22-Oct-10 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 23 Oct 2010
Article Copyright 2010 by Rajesh Anuhya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid