tags:

views:

1330

answers:

7

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

A: 

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

kenny
+8  A: 

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.

davebug
Dugg, brilliant suggestion which doesn't effect the actual data.
questzen
+19  A: 

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

harriyott
Also N'Gomo Doe.
DJClayworth
That's a good one!
harriyott
Oooh! And John-Joe Doe
harriyott
you also have variants like John McDoe vs John MacDoe
ShoeLace
Being aware of the different capitalization formats is a great point, but you didn't offer up any suggestions to cope?
Scott Saad
I typically leave it in the case the user entered, then add ucwords ... otherwise, if someone enters an acronym, you shoot yourself in the foot.
Cory Dee
@Scott: obviously the subtle suggestion is "Don't do it!".
Milen A. Radev
I agree with Cory, it's unreasonable to try to build this logic into software, it's not even deterministic as two people with the same exact characters in their last name may capitalize it differently. Leave it in the user's hands and make sure you persist their capitalization preferences.
Wedge
+12  A: 

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.

Jacco
+3  A: 

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

Rimas Kudelis
A: 

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
JosephStyons
+1  A: 

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
conny