Seehyung Lee
Seehyung Lee

Reputation: 610

Getting Full Name Query

I'm trying to get employee's full name and combine them using MySQL function "Concat". Some of our employee don't have middle name and in this case SQL throws an error. How can i get full name of an employee even if the employee doesn't have middle initial.

  SELECT CONCAT(`Employee`.`F_NAME`,
                 ' ', 
                 LEFT(`Employee`.`M_NAME`, 1),
                 '. ', 
                `Employee`.`L_NAME`) 
  FROM `Employee`

Upvotes: 1

Views: 106

Answers (1)

valex
valex

Reputation: 24144

Try to use IFNULL

  SELECT CONCAT(`Employee`.`F_NAME`,
                 ' ', 
                 IFNULL(CONCAT(LEFT(`Employee`.`M_NAME`, 1),'. '),''), 
                `Employee`.`L_NAME`) 
  FROM `Employee`

Upvotes: 1

Related Questions