Last active
February 25, 2019 19:40
-
-
Save alexdwhite/4d7c07b8f5eb72fff1a9764a31d39feb to your computer and use it in GitHub Desktop.
Function to determine credit card type from card number (SQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create the function in SQL | |
CREATE FUNCTION [dbo].[GetCardType] (@CardNum varchar(100)) | |
RETURNS varchar(10) | |
AS | |
BEGIN | |
DECLARE @type varchar(10) | |
if (left(@CardNum, 1) = '4') | |
begin | |
set @type = 'VISA' | |
end | |
else if (left(@CardNum, 2) = '34') | |
begin | |
set @type = 'AMEX' | |
end | |
else if (left(@CardNum, 2) = '37') | |
begin | |
set @type = 'AMEX' | |
end | |
else if (left(@CardNum, 2) = '65') | |
begin | |
set @type = 'DISC' | |
end | |
else if (left(@CardNum, 4) = '6011') | |
begin | |
set @type = 'DISC' | |
end | |
else | |
begin | |
set @type = 'MC' | |
end | |
RETURN @type | |
END | |
-- use the function in a query | |
Select t.CardNumber, t.ExpirationDate, dbo.GetCardType(t.CardNumber) as CardType | |
From CardTable t | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment