Skip to content

Instantly share code, notes, and snippets.

@alexdwhite
Last active February 25, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdwhite/4d7c07b8f5eb72fff1a9764a31d39feb to your computer and use it in GitHub Desktop.
Save alexdwhite/4d7c07b8f5eb72fff1a9764a31d39feb to your computer and use it in GitHub Desktop.
Function to determine credit card type from card number (SQL)
-- 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