I have fname and lname in my database, and a name could be stored as JOHN DOE or john DOE or JoHN dOE, but ultimately I want to display it as John Doe
fname being John and lname being Doe
I have fname and lname in my database, and a name could be stored as JOHN DOE or john DOE or JoHN dOE, but ultimately I want to display it as John Doe
fname being John and lname being Doe
Change the names to lower and then add ('A' - 'a') to the first letter of fname & lname.
I'm not sure where and how you want to display it, but if it's on a web-page you might try simply altering the display using CSS. Try adding:
text-transform:capitalize;
to the relevant rule, like this:
.name { text-transform:capitalize;}
if you put the name in a div or span with class="name". Of course, this will not in any way alter the database entry, but I wasn't clear which was preferable.
Be aware that there are other capitalisation formats, such as John McDoe, John D'Oe, John de O, and John van den Doe.
seeing it is tagged PHP:
either
string ucfirst ( string $str );
to uppercase first letter of the first word
or
string ucwords ( string $str );
to uppercase the first letter of every word
you might want to use those in combination with
string strtolower ( string $str );
to normalize all names to lower case first.
An example from php.net:
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
Bear in mind the notes about the locales though...
For simple names, this will work. Beware of special cases (like "Ronald McDonald" in the below example).
In SQL Server:
SELECT --step 2: combine broken parts into a final name
NAME_PARTS.FNAME_INITIAL + NAME_PARTS.REST_OF_FNAME AS FNAME
,NAME_PARTS.LNAME_INITIAL + NAME_PARTS.REST_OF_LNAME AS LNAME
FROM
( --step 1: break name into 1st letter and "everything else"
SELECT
UPPER(SUBSTRING(TEST.FNAME,1,1)) AS FNAME_INITIAL
,UPPER(SUBSTRING(TEST.LNAME,1,1)) AS LNAME_INITIAL
,LOWER(SUBSTRING(TEST.FNAME,2,LEN(TEST.FNAME))) AS REST_OF_FNAME
,LOWER(SUBSTRING(TEST.LNAME,2,LEN(TEST.LNAME))) AS REST_OF_LNAME
FROM
( --step 0: generate some test data
SELECT 'john' AS FNAME, 'doe' as LNAME
UNION SELECT 'SUZY', 'SMITH'
UNION SELECT 'bIlLy', 'BOb'
UNION SELECT 'RoNALD', 'McDonald'
UNION SELECT 'Edward', NULL
UNION SELECT NULL, 'Jones'
) TEST
) NAME_PARTS
In Oracle
SELECT --step 2: combine broken parts into a final name
NAME_PARTS.FNAME_INITIAL || NAME_PARTS.REST_OF_FNAME AS FNAME
,NAME_PARTS.LNAME_INITIAL || NAME_PARTS.REST_OF_LNAME AS LNAME
FROM
( --step 1: break name into 1st letter and "everything else"
SELECT
UPPER(SUBSTR(TEST.FNAME,1,1)) AS FNAME_INITIAL
,UPPER(SUBSTR(TEST.LNAME,1,1)) AS LNAME_INITIAL
,LOWER(SUBSTR(TEST.FNAME,2,LENGTH(TEST.FNAME))) AS REST_OF_FNAME
,LOWER(SUBSTR(TEST.LNAME,2,LENGTH(TEST.LNAME))) AS REST_OF_LNAME
FROM
( --step 0: generate some test data
SELECT 'john' AS FNAME, 'doe' as LNAME FROM DUAL
UNION SELECT 'SUZY', 'SMITH' FROM DUAL
UNION SELECT 'bIlLy', 'BOb' FROM DUAL
UNION SELECT 'RoNALD', 'McDonald' FROM DUAL
UNION SELECT 'Edward', NULL FROM DUAL
UNION SELECT NULL, 'Jones' FROM DUAL
) TEST
) NAME_PARTS
Combining PHP shorthand functions strtolower and ucwords solves your problem:
function ucname($f, $l)
{
return ucwords(strtolower($f." ".$l));
}
echo ucname($fname, $lname);
On a side note, keep in mind that you can do that sort of data beautification at many different stages: