Brad
Brad

Reputation: 12262

How do I display the first letter as uppercase?

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

Upvotes: 6

Views: 3486

Answers (7)

conny
conny

Reputation: 10145

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:

  1. before insertion, in your application
  2. during insertion, with string functions in the SQL insert/update query
  3. during extraction, with string functions in the SQL select query
  4. after extraction, in your application

Upvotes: 1

Jacco
Jacco

Reputation: 23759

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.

Upvotes: 14

JosephStyons
JosephStyons

Reputation: 58695

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

Upvotes: 1

Rimas Kudelis
Rimas Kudelis

Reputation: 564

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...

Upvotes: 3

harriyott
harriyott

Reputation: 10645

Be aware that there are other capitalisation formats, such as John McDoe, John D'Oe, John de O, and John van den Doe.

Upvotes: 21

Dave Rutledge
Dave Rutledge

Reputation: 5535

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.

Upvotes: 13

kenny
kenny

Reputation: 22344

Change the names to lower and then add ('A' - 'a') to the first letter of fname & lname.

Upvotes: -1

Related Questions