I'm attempting to script a credit card number in sets of four, going up to sixteen digits. I'm using CONCAT and SUBSTRING to do this.
Notes: I did not receive an error, it ran but the output was not what I wanted.
What I wanted was this: xxxx-xxxx-xxxx-xxxx What I got: 2147-4836-47- (Not just in one spot, but in all four entries)
Is this occurring because I am attempting to break up an int variable in subscript statement? Can you use int characters in a substring()? What about BIGINT?
Regardless of why I changed it to a varchar, and ran it with CONCAT and SUBSCRIPT. All appears to be well, but further input is welcomed. :)
Created Table:
CREATE TABLE credit_cards
(
credit_card_ID int(10) UNIQUE,
credit_card_num int(20) NOT NULL,
exp_date DATE NOT NULL,
card_fname varchar(35) NOT NULL,
card_lname varchar(35) NOT NULL,
security_code int(5) NOT NULL,
bill_street varchar(50) NOT NULL,
bill_city varchar(25) NOT NULL,
bill_state varchar(2),
bill_zip int(10) NOT NULL,
PRIMARY KEY (credit_card_ID)
);
Insert Data:
INSERT INTO credit_cards VALUES
(1, 0025184796520000, '2016-08-13', 'Sarah', 'Jones', 3351, '2490 Paseo Verde parkway, suite 150', 'San Diego','CA',92124),
(2, 7896541232548526, '2017-09-21', 'Desmond', 'Lowell', 1204, '3201 Kelsey Street, suite 109', 'San Diego','CA',92174),
(3, 1234567890123456, '2018-02-11', 'Mark', 'Jefferson', 1591, '876 Silverado Street, suite 304', 'Henderson','NV',89162),
(4, 4001330852539605, '2017-01-10', 'Jaime', 'Evans', 8879, '924 Shady Pines Circle, suite 120', 'Summerlin','NV',89074);
Select Data
SELECT credit_card_ID as 'Card ID',
CONCAT(
SUBSTRING(credit_card_num, 1,4), '-',
SUBSTRING(credit_card_num, 5,4), '-',
SUBSTRING(credit_card_num, 9,4), '-',
SUBSTRING(credit_card_num, 13,4)) as 'Card Number',
security_code as 'Security Code',
DATE_FORMAT(exp_date, '%m-%d-%Y') as 'Expiration Date',
CONCAT(card_fname, ' ', card_lname) as 'Customer Name',
CONCAT(bill_street, ', ', bill_city, ', ', bill_state, ' ', bill_zip) as 'Billing Address'
FROM credit_cards
ORDER BY exp_date ASC;