/* 
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
/**
 * Author:  Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 * Created: 10 nov. 2021
 */
DROP FUNCTION IF EXISTS `buildFullPersonName`;
-- --------------------------------------------------------
--
-- Estructura para la función `buildFullPersonName`
--
DELIMITER //
CREATE FUNCTION `buildFullPersonName`(
    first_name varchar(25),
    second_name varchar(25) ,
    last_name varchar(25) ,
    middle_name varchar(25))
    RETURNS VARCHAR (103) DETERMINISTIC
    RETURN 
        CONCAT(
            CASE
                WHEN first_name IS NOT NULL AND first_name != ''
                THEN CONCAT(SUBSTR(UPPER(first_name), 1, 1),SUBSTR(LOWER(first_name), 2), ' ')
                ELSE ''
            END,
            CASE
                WHEN second_name IS NOT NULL AND second_name != ''
                THEN CONCAT(SUBSTR(UPPER(second_name), 1, 1),SUBSTR(LOWER(second_name), 2), ' ')
                ELSE ''
            END, 
            CASE
                WHEN last_name IS NOT NULL AND last_name != ''
                THEN CONCAT(SUBSTR(UPPER(last_name), 1, 1),SUBSTR(LOWER(last_name), 2), ' ')
                ELSE ''
            END,
            CASE
                WHEN middle_name IS NOT NULL AND middle_name != ''
                THEN CONCAT(SUBSTR(UPPER(middle_name), 1, 1),SUBSTR(LOWER(middle_name), 2))
                ELSE ''
            END
        )
//
DELIMITER ;