Skip to content

Instantly share code, notes, and snippets.

@alexdwhite

alexdwhite/GetCardType.sql

Last active Feb 25, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.