string - How to get initials from fullname in MySQL? -


i have simple table:

id | fullname ---------------------------  1 | ivanov ivan ivanovich ---------------------------  2 | petrov petr petrovich ---------------------------  3 | alym kyzy ainura 

i need transform them this:

id | name_initials --------------------  1 | ivanov i. i. --------------------  2 | petrov p. p. --------------------  3 | alym k. a. 

i can make via php, mysql data manipulation. think, might (and must) done dbms only.

how can done via pure sql? should write stored function this, or there shorter way?

regards.

you use query this:

select   fullname,   concat_ws(' ',     substring_index(fullname, ' ', 1),     case when length(fullname)-length(replace(fullname,' ',''))>2       concat(left(substring_index(fullname, ' ', -3), 1), '.')     end,     case when length(fullname)-length(replace(fullname,' ',''))>1       concat(left(substring_index(fullname, ' ', -2), 1), '.')     end,     case when length(fullname)-length(replace(fullname,' ',''))>0       concat(left(substring_index(fullname, ' ', -1), 1), '.')     end) shortname   names 

please see fiddle here. query support @ maximum 4 names.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -