1

I have the following table:

CREATE TABLE `vendor_contacts` (
  `vendor_id` int(11) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `name_initials` varchar(45) NOT NULL,
  PRIMARY KEY (`vendor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

With the following insert statement:

INSERT INTO `vendor_contacts`
    VALUES (5,'Davison','Michelle',''),
           (12,'Mayteh','Kendall',''),
           (17,'Onandonga','Bruce',''),
           (44,'Antavius','Anthony',''),
           (76,'Bradlee','Danny',''),
           (94,'Suscipe','Reynaldo',''),
           (101,'O\'Sullivan','Geraldine',''),
           (123,'Bucket','Charles','');

I would like to run a query that extracts the first letter from the first name and last name columns.

SELECT vendor_id, last_name, first_name,  substring(first_name, 1, 1) AS initials
FROM vendor_contacts;

The following guide http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php, only shows how to work with one column.

1 Answer 1

3

You pull them separately and combine them using concat():

SELECT vendor_id, last_name, first_name,
       CONCAT(LEFT(first_name, 1), LEFT(last_name, 1)) as initials
FROM vendor_contacts;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.