Get initials of names

I have thousands of names in a Player Name column, and I'm wanting to store within another column the initials.

For example the following names would convert to the names on the right:

John McCarthy Blackham > JM Blackham
Nathaniel Frampton Davis Thomson > NFD Thomson
Henry Jupp -> H Jupp

How could this be done, so I could run something like this?

Update Player
Set PlayerNameInitials = getinitials()

Open in new window

PeterErhardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
You could create a stored function getinitials which has an input varchar parameter (the full name) and returns your desired result.
The stored function can use a repeat... until loop.

Check this example: http://stackoverflow.com/questions/14950466/how-to-split-the-name-string-in-mysql
Here is the MySQL documentation for Create Function and Repeat syntax.
0
Cornelia YoderArtistCommented:
I assume the delimiters are always a space?


In MySQL, you could use SUBSTRING_INDEX() to find the last space and SUBSTRING() to pick out the last name, then repeat that on the earlier words, but I think you'd need a CASE() to handle 1, 2, 3, or 4 part names (could there be 5?).  SUBSTRING() can then give you the first letter of each word.

Do you have to do this in MySQL query?  If it's a one time thing, it would be much easier to do it in php.

<?php

$player = "Nathaniel Frampton Davis Thomson";
$array = explode(" ",$player);
$newname="";
for ($i=0;$i<count($array)-1;$i++)
{
  $newname .= substr($array[$i],0,1);
}
$newname .= " ".$array[count($array)-1];

echo $newname;
?>
0
ArgentiCommented:
Here you have the stored function code:
USE `my_own_database`;
DROP function IF EXISTS `get_shortened_name`;

DELIMITER $$
USE `test`$$
CREATE FUNCTION `get_shortened_name`(full_name varchar(80)) RETURNS varchar(50) 
BEGIN
	declare nPos int;
	declare sWord varchar(25);
	declare sResult varchar(50); 
	declare sTemp varchar(80);
	set nPos = 0;
    set sTemp = trim(full_name);
	set sResult = '';

while locate(' ', sTemp) > 0 do
	set nPos = locate(' ', sTemp); -- find the position of ' '
	set sResult = concat(sResult, upper(left(sTemp, 1)));
	set sTemp = right(sTemp, length(sTemp) - nPos);	-- remaining part
end while;

	set sResult = concat(sResult, ' ', sTemp);

RETURN sResult;
END$$

DELIMITER ;

Open in new window


then call it in your query:
SELECT FullName, get_shortened_name(FullName) 
FROM NamesTable;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterErhardAuthor Commented:
Awesome function, thanks so much. That works an absolute treat :)
0
ArgentiCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.