php - MySQL stored procedure or function to return a person's name in a specific format -


i'm little fuzzy on difference between stored procedures , functions. have employee directory database data table containing, among other details, following name fields:

first_name, varchar(45) mi, char(3) last_name, varchar(45) suffix, varchar(10) 

i'd able have function/procedure return full name in

last_name (suffix), first_name (mi)

format, fields in parentheses null optional in table. can build formatted name string in php

select last_name, suffix, first_name, mi employee; 

, use series of conditionals test empty suffix , mi fields. but, i'd able use sql statement like

select getdisplayname() last_name '%smith%'; 

to return result like

smith iii, john q smith, susanne l smithers, waylon 

can point me in right direction or @ least tell me if need function or procedure?

thanks.

you can use function:

drop function if exists getdisplayname; delimiter $$ create function getdisplayname(last_name text, suffix text, first_name text, mi text)   returns text begin   declare name text;   set name = '';   if last_name not null       set name = last_name;   end if;    if suffix not null       set name = concat(name, ' ', suffix);   end if;    if first_name not null       set name = concat(name, ', ', first_name);   end if;    if mi not null       set name = concat(name, ' ', mi);   end if;    return name; end; $$ delimiter ; 

so select like:

select getdisplayname(last_name, suffix, first_name, mi) employee last_name '%smith%'; 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -